每次我运行下面的脚本我都会
无法将参数绑定到参数' FilePath'因为它是空的。
昨晚工作了。没有做出任何改变,今天早上它就失败了。有趣的是,如果我保存脚本然后运行它,它的工作原理。但是,如果我清除控制台然后再次运行它就会失败。
我错过了一些明显的东西吗?
New-Item -ItemType Directory -Force -Path C:\NonStandard_Services
set-location C:\NonStandard_Services
$Computers= Get-Content C:\computers.txt
$Report= $file
$file= $Computer
ForEach ($Computer in $Computers)
{
Get-WmiObject -ComputerName $Computer -class Win32_Service -ErrorAction SilentlyContinue |
Where-Object -FilterScript {$_.StartName -ne "LocalSystem"}|
Where-Object -FilterScript {$_.StartName -ne "NT AUTHORITY\NetworkService"} |
Where-Object -FilterScript {$_.StartName -ne "NT AUTHORITY\LocalService"} |
Select-Object -Property StartName,Name,DisplayName|
ConvertTo-Html -Property StartName,Name,DisplayName -head $HTML -body "<H2>Non- Standard Service Accounts on $Computer</H2>"| Out-File $Report -Append}
#Rename-Item c:\GP_Services\Report.htm $file
Get-ChildItem | Where-Object {$_.extension -ne ".htm"} | Rename-Item -newname { $_.name + '.htm' }
答案 0 :(得分:3)
$Report= $file $file= $Computer ForEach ($Computer in $Computers) { ... }
您可以在自己为变量赋值之前将变量分配给其他变量。将上述内容更改为:
ForEach ($Computer in $Computers) {
$file = $Computer
$Report = $file
...
}
或直接在$Computer
中使用Out-File
:
... | Out-File "$Computer.txt" -Append