从 CSV 数据循环调用 New-Item 时出错
Error calling New-Item in a loop from CSV data
在我们开始编写代码之前,我有一个 CSV 文件,其中包含 PC 列表并且只有 1 列。目标是使用 A 列中标记为 "Computers" 的信息,并获取这些机器的所有服务,并将它们输出到根据 A 列中的信息创建的文件夹中。这是目前的脚本:
Import-Module activedirectory
$Group=import-csv C:\Users\axb3055\Documents\CSV_Test.csv
$ServiceList = Test-Path C:\ServiceList
if($ServiceList -eq $true)
{Write-Host "Service List Exists"}
else
{
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList$CompName -ItemType directory -ErrorActionSilentlyContinue | Out-Null
}
$Group | foreach {$CompName = $_.Computers New-Item -Path C:\ServiceList$CompName -ItemType directory | Get-Service -ComputerName $_.Computers} | Out-File C:\ServiceList$CompName$CompName.txt
现在发生的事情是创建了服务列表文件夹,但之后没有任何反应。我在 for-each 块中得到一个指向 "New-Item" 的错误点,但我不确定它可能是什么。有什么想法吗?
我会通过分解你的管道语句来开始故障排除。打印出整个序列中的值以查看是否有空值并进行相应调整。
Import-Module activedirectory
$Group=import-csv C:\Users\axb3055\Documents\CSV_Test.csv
$ServiceList = Test-Path C:\ServiceList
if($ServiceList -eq $true)
{
Write-Host "Service List Exists"
}
else
{
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList$CompName -ItemType directory -ErrorActionSilentlyContinue | Out-Null
}
foreach ($CompName in $Group.Computers){
#create file
New-Item -Path C:\ServiceList$CompName -ItemType directory
#get service and assign to variable
$service = Get-Service -ComputerName $CompName
#output the service content to the textfile
Out-File C:\ServiceList$CompName$CompName.txt -value $service
}
还有一些改进的余地。您的直接问题是您没有将 New-Item
cmdlet 与其周围的代码分开。使用适当的缩进和换行符。如果功能和可读性受到影响,单行代码是无用的。
$Group = Import-Csv "C:\Users\axb3055\Documents\CSV_Test.csv"
$serviceListPath = "C:\ServiceList"
if(Test-Path $serviceListPath){
Write-Host "Service List Exists"
} else {
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList -ItemType directory | Out-Null
}
# Gather info and place int file
ForEach($computer in ($Group.Computers)){
# Define output folder for this computer
$outputFolder = "$serviceListPath$computer"
$outputFile = "$outputFolder$computer.csv"
# Create the output folder if it does not already exist.
if(!(Test-Path $outputFolder)){New-Item -Path $outputFolder -ItemType directory | Out-Null}
# Output Service information to file.
Get-Service -ComputerName $computer | Select-Object Status,Name,DisplayName | Export-CSV -NoTypeInformation $outputFile
}
这应该检查通过的每台计算机的服务,并将结果以 CSV 格式记录在各自的文件夹中。
在我们开始编写代码之前,我有一个 CSV 文件,其中包含 PC 列表并且只有 1 列。目标是使用 A 列中标记为 "Computers" 的信息,并获取这些机器的所有服务,并将它们输出到根据 A 列中的信息创建的文件夹中。这是目前的脚本:
Import-Module activedirectory
$Group=import-csv C:\Users\axb3055\Documents\CSV_Test.csv
$ServiceList = Test-Path C:\ServiceList
if($ServiceList -eq $true)
{Write-Host "Service List Exists"}
else
{
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList$CompName -ItemType directory -ErrorActionSilentlyContinue | Out-Null
}
$Group | foreach {$CompName = $_.Computers New-Item -Path C:\ServiceList$CompName -ItemType directory | Get-Service -ComputerName $_.Computers} | Out-File C:\ServiceList$CompName$CompName.txt
现在发生的事情是创建了服务列表文件夹,但之后没有任何反应。我在 for-each 块中得到一个指向 "New-Item" 的错误点,但我不确定它可能是什么。有什么想法吗?
我会通过分解你的管道语句来开始故障排除。打印出整个序列中的值以查看是否有空值并进行相应调整。
Import-Module activedirectory
$Group=import-csv C:\Users\axb3055\Documents\CSV_Test.csv
$ServiceList = Test-Path C:\ServiceList
if($ServiceList -eq $true)
{
Write-Host "Service List Exists"
}
else
{
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList$CompName -ItemType directory -ErrorActionSilentlyContinue | Out-Null
}
foreach ($CompName in $Group.Computers){
#create file
New-Item -Path C:\ServiceList$CompName -ItemType directory
#get service and assign to variable
$service = Get-Service -ComputerName $CompName
#output the service content to the textfile
Out-File C:\ServiceList$CompName$CompName.txt -value $service
}
还有一些改进的余地。您的直接问题是您没有将 New-Item
cmdlet 与其周围的代码分开。使用适当的缩进和换行符。如果功能和可读性受到影响,单行代码是无用的。
$Group = Import-Csv "C:\Users\axb3055\Documents\CSV_Test.csv"
$serviceListPath = "C:\ServiceList"
if(Test-Path $serviceListPath){
Write-Host "Service List Exists"
} else {
Write-Host "Creating ServiceList folder"
New-Item C:\ServiceList -ItemType directory | Out-Null
}
# Gather info and place int file
ForEach($computer in ($Group.Computers)){
# Define output folder for this computer
$outputFolder = "$serviceListPath$computer"
$outputFile = "$outputFolder$computer.csv"
# Create the output folder if it does not already exist.
if(!(Test-Path $outputFolder)){New-Item -Path $outputFolder -ItemType directory | Out-Null}
# Output Service information to file.
Get-Service -ComputerName $computer | Select-Object Status,Name,DisplayName | Export-CSV -NoTypeInformation $outputFile
}
这应该检查通过的每台计算机的服务,并将结果以 CSV 格式记录在各自的文件夹中。