JessieBear
JessieBear

Reputation: 131

Calling param object inside script block

I am trying to create a folder remotely but the parameter that I type in command line doesn't pass in script block. Below code will only create directory webapp.

$computerName = "s3apdev0074"
Invoke-Command -ComputerName $computerName -ScriptBlock { param([string]$appname) md d:\webapp\$appname}

Upvotes: 0

Views: 56

Answers (1)

DarkLite1
DarkLite1

Reputation: 14705

Please try this:

$ComputerName = 's3apdev0074'
$AppName = Read-Host "Please enter the application name"

Invoke-Command -ComputerName $ComputerName -ArgumentList $AppName -ScriptBlock {
    Param (
        [String]$AppName
    )
    # DOS: 
    md d:\webapp\$AppName

    # PowerShell: New-Item -Path "d:\webapp\$AppName" -ItemType Directory
}

It seem you are only missing the -ArgumentList parameter to pass in your local variable.

More info here:

Get-Help Invoke-Command -Parameter ArgumentList

Upvotes: 1

Related Questions