locked
Scope of $using RRS feed

  • Question

  • Hi All, 

    Below is a sample snippet. I am bit confused on the $using, seems does not flow down. Here I do not get any value. 

    Can anyone let me know what is the issue here. 

    Write-Output "$using:ServerName- Inside Invoke"
    workflow SampleConnTest
    {
    $ServerName = "Test12"
    $AutomationConnection = Get-AutomationConnection –Name "MyConn"
    $SvcActPwd = ConvertTo-SecureString –AsPlainText –String $AutomationConnection.Password –Force  
    $SvcActCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $AutomationConnection.Username, $SvcActPwd
    Write-output "$ServerName - OutSideinline" 
    InlineScript {
    write-output "$using:ServerName- Inside Inline"
    Invoke-Command -ComputerName "Testserver" -Credential $using:SvcActCredential -ScriptBlock {            
    Write-Output "$using:ServerName- Inside Invoke"
    }     
    } -PSCredential $SvcActCredential
    }
    Regards


    Priyabrata

    Tuesday, July 25, 2017 8:55 AM

All replies

  • What is the error?

    "$using" is only valid in remote commands.


    \_(ツ)_/

    Tuesday, July 25, 2017 9:28 AM
  • There are no visible errors. The value is not passed to the invoke command. where are available in Inline{}

    Invoke-Command -ComputerName "Testserver" -Credential $using:SvcActCredential -ScriptBlock {            
    Write-Output "$using:ServerName- Inside Invoke"
    } 

    So I am bit confused with the behavior of the variable.


    Priyabrata

    Tuesday, July 25, 2017 9:31 AM
  • It is not valid as used: 

    This will show you what works and what doesn't.

    workflow SampleConnTest {
    	$ServerName = 'ws702'
    	Write-output "$ServerName - OutSideinline"
    	InlineScript {
    		write-output "$using:ServerName- Inside Inline"
    		Invoke-Command -ComputerName $using:ServerName -ScriptBlock {
    			# the following line will not work because it is run on the remote system
    			Write-Output "$using:ServerName- Inside Invoke"
    		}
    	}
    }


    \_(ツ)_/



    • Edited by jrv Tuesday, July 25, 2017 9:43 AM
    Tuesday, July 25, 2017 9:34 AM
  • Thanks for the hint. I just got the below line. Works fine. I missed this completely. 

    -PSComputerName "$using:ServerName" 
    workflow SampleConnTest
    {
    $ServerName = "Test12"
    $AutomationConnection = Get-AutomationConnection –Name "MyConn"
    $SvcActPwd = ConvertTo-SecureString –AsPlainText –String $AutomationConnection.Password –Force  
    $SvcActCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $AutomationConnection.Username, $SvcActPwd
    Write-output "$ServerName - OutSideinline" 
    InlineScript {
    write-output "$using:ServerName- Inside Inline"
      
    }-PSComputerName "$using:ServerName"  -PSCredential $SvcActCredential
    }
    Regards


    Priyabrata

    Tuesday, July 25, 2017 10:09 AM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, August 11, 2017 4:36 AM