Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

powershell - How do I pass a local variable to a remote `Invoke-Command`?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:

Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash E:est$Using:dest.zip -Algorithm SHA1 
}

For a solution that works with all versions:

Invoke-Command -ComputerName winserver -ScriptBlock {
    param($myDest)

    Get-FileHash E:est$myDest.zip -Algorithm SHA1 
} -ArgumentList $dest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...