none
Calling parameters in a scriptblock to run against another powershell script RRS feed

  • Question

  • Hi,

    I am writing a script that will take in some parameter values and pass those to my commands. One of my values is a name that I'd like to pass into a scriptblock command to use with another powershell script that I have prestaged on a virtual machine. Is there a way to pass that parameter to the scriptblock and to the other powershell on the VM? Code below.

    Thanks,
    Georgi

    # Parameters
     PARAM(
     [ValidateSet("DBs.csv", "DBsAXOnly.csv")]
     [String]
     $csvfile,
     [parameter(Mandatory=$true)]
     [String]
     $cloudName,
     [parameter(Mandatory=$true)]
     [String]
     $vmName
     )
    
    
    $script = {
    Param($param1)
    $csvfile = $param1
    #import-module Storage
    $storagePool1 = Get-PhysicalDisk -FriendlyName PhysicalDisk4, PhysicalDisk3, PhysicalDisk5, PhysicalDisk6 -CanPool $true
    $storagePool2 = Get-PhysicalDisk -FriendlyName PhysicalDisk7, PhysicalDisk8, PhysicalDisk9 -canPool $true
    new-StoragePool -FriendlyName "SQLData" -StorageSubsystemFriendlyName "Storage Spaces*" -PhysicalDisks $storagePool1 |New-VirtualDisk -FriendlyName "SQLData" -ProvisioningType Fixed -UseMaximumSize -ResiliencySettingName Simple |Initialize-Disk -PassThru |New-Partition -DriveLetter K -UseMaximumSize |Format-Volume -AllocationUnitSize 65536 -force
    new-storagePool -FriendlyName "SQLLogs" -StorageSubsystemFriendlyName "Storage Spaces*" -PhysicalDisks $storagepool2 |New-virtualDisk -FriendlyName "SQLLogs" -ProvisioningType Fixed -UseMaximumSize -ResiliencySettingName Simple |Initialize-Disk -Passthru |New-Partition -DriveLetter L -UseMaximumSize |Format-Volume -AllocationUnitSize 65536 -force
    invoke-command -FilePath c:\scripts\SQLAutoMover.ps1 -csvfile $csvfile
    }
    
    
    $endpoint = Get-AzureVM -ServiceName "$cloudname" -Name "$vmName" | Get-AzureEndpoint -Name PowerShell
    $session = new-PSSession -ComputerName "$CloudName.cloudapp.net" -Credential (Get-Credential) -Port $endpoint.Port -UseSSL
    Invoke-Command -Session $session -Script $script -ArgumentList $csvfile
    
    


    Georgi

    Friday, June 5, 2015 5:35 PM

Answers

  • Hi,

    I ended up going a different route with this script. After several attempts to pass the parameter into the $script section that would run on the remote host, I decided to just copy the remote host powershell script code into the $script section. My script then uses the parameter as it should.

    Thanks,
    Georgi


    Georgi

    • Marked as answer by Gmeyer Tuesday, June 9, 2015 3:01 AM
    Tuesday, June 9, 2015 3:01 AM

All replies

  • Just pass it.  Why is that an issue.  That is what parameters are for.

    I see no issue with you calls. 



    \_(ツ)_/


    • Edited by jrv Friday, June 5, 2015 10:06 PM
    Friday, June 5, 2015 10:05 PM
  • Hi,

    It's this part of the script that does not use the $csvfile parameter.

    Invoke-Command -Session $session -Script $script -ArgumentList $csvfile

    $script = {
    Param($param1)
    $csvfile = $param1
    #import-module Storage
    $storagePool1 = Get-PhysicalDisk -FriendlyName PhysicalDisk4, PhysicalDisk3, PhysicalDisk5, PhysicalDisk6 -CanPool $true
    $storagePool2 = Get-PhysicalDisk -FriendlyName PhysicalDisk7, PhysicalDisk8, PhysicalDisk9 -canPool $true
    new-StoragePool -FriendlyName "SQLData" -StorageSubsystemFriendlyName "Storage Spaces*" -PhysicalDisks $storagePool1 |New-VirtualDisk -FriendlyName "SQLData" -ProvisioningType Fixed -UseMaximumSize -ResiliencySettingName Simple |Initialize-Disk -PassThru |New-Partition -DriveLetter K -UseMaximumSize |Format-Volume -AllocationUnitSize 65536 -force
    new-storagePool -FriendlyName "SQLLogs" -StorageSubsystemFriendlyName "Storage Spaces*" -PhysicalDisks $storagepool2 |New-virtualDisk -FriendlyName "SQLLogs" -ProvisioningType Fixed -UseMaximumSize -ResiliencySettingName Simple |Initialize-Disk -Passthru |New-Partition -DriveLetter L -UseMaximumSize |Format-Volume -AllocationUnitSize 65536 -force
    invoke-command -FilePath c:\scripts\SQLAutoMover.ps1 -csvfile $csvfile
    }
    
    
    $endpoint = Get-AzureVM -ServiceName "$cloudname" -Name "$vmName" | Get-AzureEndpoint -Name PowerShell
    $session = new-PSSession -ComputerName "$CloudName.cloudapp.net" -Credential (Get-Credential) -Port $endpoint.Port -UseSSL
    Invoke-Command -Session $session -Script $script -ArgumentList $csvfile


    Georgi

    Friday, June 5, 2015 10:10 PM
  • I have no idea what that is supposed to mean.  It is not even a complete sentence. What part?  Try to be clear.

    \_(ツ)_/

    Friday, June 5, 2015 10:22 PM
  • Hi,

    In my script I am using this as a parameter, $csvfile. When I call my script I use script.ps1 -csvfile csvfilename. It's the $csvfile parameter that does get called in the $script portion of the powershell code.

    In $script I call a powershell script, invoke-command -filepath c:\scripts\sqlautomover.ps1 -csvfile $csvfile, that is on the virtual machine itself which uses the $csvfile parameter. it's that name that I want to pass into $script that will be used in the invoke-command -filepath c:\scripts\sqlautomover.ps1 -csvfile $csvfile

    As you can see in the initial script that I posted, I have a parameter list, then the $script piece, and then where I invoke a possession to use the commands on a remote server.


    Georgi

    Friday, June 5, 2015 10:41 PM
  • It works fine for me:

    $script = {
        return $args[0]
    }
    
    Invoke-Command -Script $script -ArgumentList 'hello world'

    What makes you think it doesn't work?


    \_(ツ)_/

    Friday, June 5, 2015 11:33 PM
  • When I run the command I receive an error that $csvfile does not exist as a parameter.

    A parameter cannot be found that matches parameter name 'csvfile'.
        + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
        + PSComputerName        : ax2012r3-demo-mbsdaxtest1-a682e3b16ccdcc71.cloudapp.net


    Georgi

    Saturday, June 6, 2015 12:58 AM
  • That is exctly right. There is no such  named parameter on Invoke-Command.

    invoke-command -FilePath c:\scripts\SQLAutoMover.ps1 -csvfile $csvfile

    Why wouyld you think that ther ewould be a paramer of that name. 

    HELP Invoke-Command -Parameter csvfile

    No such named parameter.


    \_(ツ)_/

    Saturday, June 6, 2015 2:16 AM
  • When you're using Invoke-Command, you have to pass arguments to the script block positionally, with the -ArgumentList parameter of Invoke-Command. There is no support for passing named arguments with that parameter.  However, I don't see a reason for that "inner" call to Invoke-Command.  Why not just execute the script like this?

    c:\scripts\SQLAutoMover.ps1 -csvfile $csvfile

    Saturday, June 6, 2015 3:08 AM
  • Hi,

    I ended up going a different route with this script. After several attempts to pass the parameter into the $script section that would run on the remote host, I decided to just copy the remote host powershell script code into the $script section. My script then uses the parameter as it should.

    Thanks,
    Georgi


    Georgi

    • Marked as answer by Gmeyer Tuesday, June 9, 2015 3:01 AM
    Tuesday, June 9, 2015 3:01 AM
  • Hi,

    I ended up going a different route with this script. After several attempts to pass the parameter into the $script section that would run on the remote host, I decided to just copy the remote host powershell script code into the $script section. My script then uses the parameter as it should.

    Thanks,
    Georgi


    Georgi

    You fail to understand that there is no such argument as "-csvfile"    You can only use -Argumentlist and pass a list or use the syntax ( in V3 and later) - "$using:varname",

    Why is that so difficult.  Have you even tried to read the help for this?


    \_(ツ)_/


    • Edited by jrv Tuesday, June 9, 2015 3:43 AM
    Tuesday, June 9, 2015 3:36 AM
  • So this argument does exist in my sqlautomover.ps1 script. It also exists in the initial script so I can call it. I have updated my script to use the -argumentlist and I was still receiving the same error. The -csvfile parameter was still not going through to the local script on the virtual machine. In the script on the virtual machine I use import-csv c:\path\$csvfile. That way I can import the csv file that exists on the virtual machine. But I want to specify the csv file name as I have two different ones and separate tasks happen depending on the csv file name passed through the initial script (above). I have even removed the -csvfile in invoke-command and used -args to pass the parameter and I also removed the invoke-command command and just called the script directly where I can use the -csvfile parameter and I still received the same error that the file did not exist. What I ended up doing actually works better so that I only have to change code at one place if I need to send it back out again for use.

    Yes, I have read the help on how to do this. I read a lot of how to pass parameters before posting here. All other parameters I have ever used in all of my other scripts work as expected. This time however it was not passing on to the Powershell script on the virtual machine.

    Thank you.


    Georgi

    Tuesday, June 9, 2015 3:56 AM
  • So this argument does exist in my sqlautomover.ps1 script. It also exists in the initial script so I can call it. I have updated my script to use the -argumentlist and I was still receiving the same error. The -csvfile parameter was still not going through to the local script on the virtual machine. In the script on the virtual machine I use import-csv c:\path\$csvfile. That way I can import the csv file that exists on the virtual machine. But I want to specify the csv file name as I have two different ones and separate tasks happen depending on the csv file name passed through the initial script (above). I have even removed the -csvfile in invoke-command and used -args to pass the parameter and I also removed the invoke-command command and just called the script directly where I can use the -csvfile parameter and I still received the same error that the file did not exist. What I ended up doing actually works better so that I only have to change code at one place if I need to send it back out again for use.

    Yes, I have read the help on how to do this. I read a lot of how to pass parameters before posting here. All other parameters I have ever used in all of my other scripts work as expected. This time however it was not passing on to the Powershell script on the virtual machine.

    Thank you.


    Georgi

    The problem is that you cannot pass a named argument on an invoke.  Invoke only has -argumentlist.  Arguments are passed by position.  REad the help to understand what is possible. I am posting this so others will not be misled by your choice of answers.

    Try to erase your mind and read all of this thread.  I think you will eventually understand what the real issue is.

    There is no "-csvfile" parameter on Invoke-Command.  YOU have added a "-csvfile" parameter to that command.  That is what is causing your error.

    The file you are invoking or calling must exist on the remote machine. The initial error is because of syntax.

    All of this is caused by a lack of understanding of how PowerShell works and how "remoting" works.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 9, 2015 4:07 AM
    Tuesday, June 9, 2015 4:04 AM
  • I understand what you are saying. And that is when I changed my commands to remove the -csvfile parameter to pass the command correctly using invoke-command without the -csvfile parameter. However my parameter was still not going through as it should to the script on the remote server using the argument list method. My problem is using parameters on the remote server to use it with a powershell script on the remote server. My problem is not using invoke-command. I was simply trying invoke-command as a way to invoke the powershell script on the remote server.  

    So my original issue, and the reason for this post, is to use parameters from the initial script to pass it onto the script on the remote server. It is not using invoke-command.

    # Parameters
     PARAM(
     [ValidateSet("DBs.csv", "DBsAXOnly.csv")]
     [String]
     $csvfile,
     [parameter(Mandatory=$true)]
     [String]
     $cloudName,
     [parameter(Mandatory=$true)]
     [String]
     $vmName
     )
    $script = {
    Param($param1)
    $csvfile = $param1
    c:\scripts\SQLAutoMover.ps1 -csvfile $csvfile
    }
    $endpoint = Get-AzureVM -ServiceName "$cloudname" -Name "$vmName" | Get-AzureEndpoint -Name PowerShell
    $session = new-PSSession -ComputerName "$CloudName.cloudapp.net" -Credential (Get-Credential) -Port $endpoint.Port -UseSSL
    Invoke-Command -Session $session -Script $script
    
    OR
    
    $script = {
    Param($csvfiles)
    $csvfile = $csvfiles
    #import-module Storage
    invoke-command c:\scripts\SQLAutoMover.ps1 -ArgumentList $csvfile
    }
    
    


    Georgi

    Tuesday, June 9, 2015 4:20 AM
  • Using Invoke command in a remote session is pointless and will very often cause problems.

    To execute a script file remotely that file must exist remotely.

    Give it some time.  You will eventually see what the issue(s) is(are).


    \_(ツ)_/


    • Edited by jrv Tuesday, June 9, 2015 4:28 AM
    Tuesday, June 9, 2015 4:27 AM