gpt4 book ai didi

powershell - PowerShell-将变量传递给Invioke-Command

转载 作者:行者123 更新时间:2023-12-02 23:27:53 24 4
gpt4 key购买 nike

由于某些原因,当我尝试在下面的代码中的Get-ChildItem之后使用$ scanpath变量时,它不起作用。但是,如果我将实际路径替换为$ scanpath,它将起作用。我究竟做错了什么? $ computer和$ savepath变量都可以正常工作。

$computer = 'Server'
$scanpath = 'P$\Directory\Directory\Z'
$savepath = 'C:\Z-Media.csv'
Invoke-Command -ComputerName $computer -scriptblock {Get-ChildItem $scanpath -recurse -include *.mp3,*.wma,*.wmv,*.mov,*.mpg,*.ogg,*.jpg -force | select FullName, Length | Sort-Object { [long]$_.Length } -descending} | Export-Csv $savepath -NoTypeInformation

最佳答案

$scanpath与脚本块不在同一范围内。您有2种方法可以解决此问题:

PowerShell 3+-Using范围修改器

Invoke-Command -ComputerName $computer -scriptblock {Get-ChildItem $Using:scanpath -recurse}

有关更多信息,请参见 about_Scopes

Using is a special scope modifier that identifies a local variable in a remote command. By default, variables in remote commands are assumed to be defined in the remote session.



任何版本-参数

Invoke-Command -ComputerName $computer -scriptblock {param($thisPath) Get-ChildItem $thisPath -recurse} -ArgumentList $scanpath

您可以像功能一样给脚本块参数。 Invoke-Command带有 -ArgumentList参数,该参数将值传递到脚本块的参数中。

关于powershell - PowerShell-将变量传递给Invioke-Command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com