0

Before we get to the code, I have a CSV with a list of PC's and only 1 column. The goal is to use the info from Column A, labeled "Computers" and get all services for these machines and output them into folders created based on the info from Column A. Here is the script so far:

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

What is happening now is that the Service List folder is created but nothing happens after that. I get an error point to the "New-Item" in the for-each block but I am not sure what it could be. Any ideas?

Matt
  • 45,022
  • 8
  • 78
  • 119
  • What is the error? Also in the `else` you seem to have suppressed errors with `-ErrorActionSilentlyContinue`. Remove that for debugging. `$CompName` appears to be null so the error is the folder already exists? Where do you set `$compname` – Matt Mar 14 '16 at 16:39
  • Thanks for the fast response. I have removed the -errorAction as advised. The error I am getting is: "Unexpected Token 'New-Item' in expression or statement." That was one thing I wasn;t sure of as well, in regards to setting $compname. I wanted to set the name to the data from column A in the CSV. –  Mar 14 '16 at 17:01
  • I think you need a pipe "|" between the $_.Computers and New-Item? but I may be mistaken.. Looks awkward right now. – runcmd Mar 14 '16 at 17:01
  • @krousemw Minimum he needs is a semicolon as those appear to two separate statements. That is not how the code should be written anyway though. – Matt Mar 14 '16 at 17:03
  • @Matt any advise would be awesome cause I am kind of lost. Thanks! –  Mar 14 '16 at 17:06
  • Does the data have to be in unique files per computer? – Matt Mar 14 '16 at 17:12
  • what I was looking to do was have the script create folders based on the info from the CSV under the root folder. Within this folders, is a txt file with a list of all the services titled after the computer name, provided in the CSV. –  Mar 14 '16 at 17:13
  • Calling for the activedirectory import is also not needed here. – Matt Mar 14 '16 at 17:22

2 Answers2

1

There is some room for improvement. You immediate issue is that you are not separating the New-Item cmdlet from the code around it. Use proper indentation and newlines. One-liners are useless if functionality and readability are to suffer.

$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
}

That should check the services of each computer passed and record the results as a CSV each in it's own folder.

Matt
  • 45,022
  • 8
  • 78
  • 119
-1

I would begin troubleshooting by breaking up your piping statements. Print out values throughout the sequence to see if you have null values and tweak accordingly.

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        
    }
runcmd
  • 572
  • 2
  • 7
  • 22
  • The first `$compname` is null. Good catch on the loop definition. `New-Item` will be making some output so you might need to catch that might also have to check that the folder exists. You are dealing with objects so `Out-File` might not be the best choice here. – Matt Mar 14 '16 at 17:19