问题描述
这是我的第一个 Powershell 命令,已经一个星期了,我不知道是什么问题。我想做的是以下内容:
我的第一个问题是由于某种原因我的 Get-Content 命令似乎不起作用,
# Remote run the install for each system
foreach ($System in $SystemList) {
if (test-Connection -Cn $System -quiet) {
copy-item $SetupFolder -Destination \\$System\$Dest -recurse -Force
if (Test-Path - Path $) {
Invoke-Command -ComputerName $System -ScriptBlock {powershell.exe $Path /S} -credential $Credentials
Write-Host -ForegroundColor Green "Installation Successful on $System"
}
} else {
Write-Host -ForegroundColor Red "$System is not online,Install Failed"
}
}
arr1 = [
{
"id":4390,"name":"panadol"
},{
"id":4392,"name":"aspirin"
},{
"id":1390,"name":"vantage"
},{
"id":2390,"name":"cream"
},]
arr2 = [
{
"id":3390,"name":"morphine"
},{
"id":3231,"name":"flagyl"
},]
console.log([...arr2,...arr1])
解决方法
$FileBrowser.ShowDialog()
返回显示对话框的结果 - 而不是实际选择的文件路径。
为此,您需要$FileBrowser.FileName
:
$dialogResult = $FileBrowser.ShowDialog()
if($dialogResult -eq 'OK'){
# user selected a file
$GetList = $FileBrowser.FileName
}
else{
# user canceled the dialog,either `return`,throw an error,or assign a static value to `$GetList`,like this
$GetList = "C:\default\path\to\hostnames.txt"
}
# Now we can safely proceed with `Get-Content`:
$SystemList = Get-Content -LiteralPath $GetList