【问题标题】:Error calling New-Item in a loop from CSV data从 CSV 数据循环调用 New-Item 时出错
【发布时间】:2016-03-14 16:35:08
【问题描述】:

在我们开始编写代码之前,我有一个包含 PC 列表的 CSV,并且只有 1 列。目标是使用 A 列中标记为“计算机”的信息,并获取这些机器的所有服务,并将它们输出到基于 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”的错误点,但我不确定它可能是什么。有什么想法吗?

【问题讨论】:

  • 错误是什么?同样在else 中,您似乎使用-ErrorActionSilentlyContinue 抑制了错误。删除它以进行调试。 $CompName 似乎为空,所以错误是文件夹已经存在?你在哪里设置$compname
  • 感谢您的快速响应。我已按照建议删除了 -errorAction。我得到的错误是:“表达式或语句中出现意外的令牌'New-Item'。”关于设置 $compname,这是我也不确定的一件事。我想将名称设置为 CSV 中 A 列中的数据。
  • 我认为你需要一个管道“|”在 $_.Computers 和 New-Item 之间?但我可能弄错了..现在看起来很尴尬。
  • @krousemw 他最少需要一个分号,因为分号出现在两个单独的语句中。无论如何,这不是代码应该编写的方式。
  • @Matt 任何建议都会很棒,因为我有点迷路了。谢谢!

标签: csv powershell variables new-item


【解决方案1】:

还有一些改进的余地。您的直接问题是您没有将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 记录在各自的文件夹中。

【讨论】:

    【解决方案2】:

    我将通过分解您的管道语句开始故障排除。打印整个序列中的值以查看是否有空值并相应地进行调整。

    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        
        }
    

    【讨论】:

    • 第一个$compname 为空。很好地抓住了循环定义。 New-Item 将进行一些输出,因此您可能需要捕捉到可能还必须检查文件夹是否存在。您正在处理对象,因此 Out-File 可能不是这里的最佳选择。
    猜你喜欢
    • 2016-01-19
    • 2019-08-29
    • 1970-01-01
    • 2016-04-24
    • 2012-04-04
    • 1970-01-01
    • 2014-12-24
    • 2023-04-10
    • 2013-03-07
    相关资源
    最近更新 更多