0

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}

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

The code works but the value of $appname should be based on what I type in command line.For example .\test.ps1 app1. Thanks for the help.
Answer updated, you only need to use Read-Host to accomplish this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.