我的Powershell脚本没有写入我指定的文本文件,我无法弄清楚原因。该脚本当前登录到远程服务器,据我所知,它正在正确执行powershell作业以收集数据。我最初试图退出远程会话然后写入数据,但变量$Data
没有保留数据。
Add-PSSnapin vmware.vimautomation.core #required for vm-cmdlets
$User = [Environment]::UserName
$Workstation = [Environment]::MachineName
$Log = "\\folder\user$\name\CompasFound.txt"
$Search_Dir = Get-Content -Path C:\Users\$User\Documents\WheresMyCompass.txt
$Servers = Get-Content -Path C:\Users\$User\Documents\WheresMyWorkStation.txt
#Enter-PSSession -Computer vmname
Connect-VIServer -Server 192.168.x.x -User "user" -Pass "pass"
Function CompassFinder #Finding Compass file(s) function
{
Start-Job -Name CompFind -ScriptBlock{
$Data = Get-Childitem -Path $Search_Dir -Recurse *.doc;
#Exit-PSSession; #Breaking remote session
$Data | Format-Table -AutoSize | Add-Content $Log #Writing data
}
}
Function GenLogFinder #Finding GeneralLog file function
{
Start-Job -Name LogFind -ScriptBlock{
$GLogData = Get-Childitem -Path $Search_Dir -Recurse -Include GeneralLog.txt;
#Exit-PSSession; #Breaking remote session
$GLogData = Format-Table -AutoSize | Add-Content $Log -append; #Writing data
}
}
CompassFinder;
GenLogFinder;
答案 0 :(得分:0)
你确定你没有在这里做错字吗?
$GLogData = Format-Table
应该是|
而不是=
吗?
答案 1 :(得分:0)
必须将PowerShell变量显式传递到脚本块中:
Start-Job -ScriptBlock {
param( $Search_Dir, $Log )
$Data = Get-Childitem -Path $Search_Dir -Recurse *.doc
$Data | Format-Table -AutoSize | Add-Content $Log
} -ArgumentList $Search_Dir, $Log
否则脚本块将使用新的(空)变量$Search_Dir
和$Log
。
对于简单的脚本块,只使用$args
数组将是另一种选择:
Start-Job -ScriptBlock {
$Data = Get-Childitem -Path $args[0] -Recurse *.doc
$Data | Format-Table -AutoSize | Add-Content $args[1]
} -ArgumentList $Search_Dir, $Log
答案 2 :(得分:0)
由于Format-Table
行,我程序的当前结构是将奇数数据写入文本文件。