0

我的 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;
4

3 回答 3

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
于 2013-07-10T16:16:20.820 回答
0

由于该行,我的程序的当前结构正在将奇数数据写入文本文件Format-Table

于 2013-07-10T19:43:49.680 回答
0

你确定你没有在这里打错字吗?

$GLogData = Format-Table 

应该|代替=吗?

于 2013-07-10T16:03:04.667 回答