locked
Powershell.AddParameter is always blank RRS feed

  • Question

  • Hi guys,

    Sorry if this was answered before (couldn't find it any)

    For some reason after I powershell.addscript, my addparameter doesn't seem to be working:

    #hello.ps1
    param(
        [string]$thingToWrite
    )
    
    $helloLog = [System.IO.StreamWriter]"c:\files\hellolog.txt"
    $helloLog.WriteLine( $thingToWrite)
    $helloLog.Close()


    #run-hello.ps1
    $p = [Powershell]::Create()
    $p.AddScript("C:\files\hello.ps1")
    $p.AddParameter("thingToWrite","write this")
    $p.Invoke()
    

    My parameter is always blank. I've tried different approaches and searched various forums, but can't seem to figure it out yet. Any help at all will be greatly appreciated!!

    Monday, July 10, 2017 2:56 AM

Answers

  • Never mind, I am an idiot - using AddCommand instead of AddScript got things going
    • Marked as answer by Jason001 Monday, July 10, 2017 3:18 AM
    Monday, July 10, 2017 3:18 AM

All replies

  • Never mind, I am an idiot - using AddCommand instead of AddScript got things going
    • Marked as answer by Jason001 Monday, July 10, 2017 3:18 AM
    Monday, July 10, 2017 3:18 AM
  • The command adds a script. You are adding a string that is a filename.

    $p = [Powershell]::Create()
    $p.AddScript({
    		param (
    			[string]$thingToWrite
    		)
    		
    		$helloLog = [System.IO.StreamWriter]"c:\files\hellolog.txt"
    		$helloLog.WriteLine($thingToWrite)
    		$helloLog.Close()
    })
    $p.AddParameter("thingToWrite", "write this")
    $p.Invoke()


    \_(ツ)_/

    Monday, July 10, 2017 3:30 AM