Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

problem with csv

$
0
0

Hi everyone,

I have an odd problem. I am building an inventory script. It will pull data from various locations (AD, our Help Desk program, vCenter, WSUS, McAfee EPO, and a couple other local sources. Basically, what it will then do some combining and filtering of these lists, and tell us what items are not in WSUS, EPO etc. This is something that Management wants.

So when I run the get-QADComputer line to export the data from AD it creates the file no problem, but when it parses through the lower code it is as though there is a problem with the CSV file, but if I open the CSV file, and simply press the save key NOT CHANGING ANY DATA, and then run the code again, without generating the AD export, it runs just fine and gives me the data I am expecting. Is there something wrong with the export-csv function?

Anything will help

thanks

app

Here is the code I have

Set-Location e:\Scripts\IT_Audit_Script
$date = (Get-Date).ToString('yyyyMMdd')

# Generate the AD Inventory List
#Get-QADComputer -SearchRoot "XX.XXX.org/servers" -SizeLimit 0 | Select-Object -property Name, OperatingSystem | Export-Csv ".\ad_inventory.csv" -NoTypeInformation

# Edit the existing csv files to ensure that column names are the same

$file1 = import-csv -Path ".\Physical_Inventory.csv"
$File2 = import-csv -Path ".\VMWare_Inventory.csv"
#$file3 = import-csv -Path ".\all_master.csv"
#$file4 = import-csv -Path ".\win_master.csv"
$file5 = import-csv -Path ".\EPO_Inventory.csv"
$file6 = import-csv -Path ".\WSUS_Inventory.csv"
$file7 = import-csv -Path ".\SN_inventory.csv"
$file8 = import-csv -Path ".\ad_inventory.csv"

#Physical Inventory CSV
(Get-Content ".\Physical_inventory.csv") | 
Foreach-Object {$_ -replace "label", "computer"} | 
#Set-Content ".\Physical_inventory.csv"
#(Get-Content ".\Physical_inventory.csv") | 
Foreach-Object {$_ -replace "Service", "Description"} | 
Set-Content ".\Physical_inventory.csv"

#VmWare Inventory CSV
(get-content ".\VMWare_inventory.csv") |
Foreach-object {$_ -replace "name", "computer"} |
Foreach-Object {$_ -replace ".XXX.XXXX.org", ""} |
ForEach-Object {$_ -replace ".XX.XXX.org", ""} |
Set-content ".\VMWare_inventory.csv"

#WSUS Inventory CSV
(get-content ".\wsus_inventory.csv") |
Foreach-object {$_ -replace "FullDomainName", "computer"} |
Foreach-Object {$_ -replace ".oatest.oclc.org", ""} |
Set-content ".\wsus_inventory.csv"

#ServiceNow Inventory CSV
(get-content ".\sn_inventory.csv") |
Foreach-object {$_ -replace "name", "computer"} |
Set-content ".\sn_inventory.csv"

#AD Inventory CSV
(get-content ".\AD_inventory.csv") |
Foreach-object {$_ -replace "name", "computer"} |
Set-content ".\AD_inventory.csv"

#EPO Inventory CSV
(get-content ".\EPO_inventory.csv") |
Foreach-object {$_ -replace "System Name", "computer"} |
Set-content ".\EPO_inventory.csv"

# This section of code combines certain fields from both the Physical inventory and VMWare inventory csv into one "Master" file.
# My muse for this code came from http://ps1scripting.blogspot.com/2012/07/powershell-working-with-csv-files.html

# This subsection pulls out all servers with Windows and CentOS.

Echo "Computer,Description,OS,Origin" | Out-file ".\all_master.txt"

Foreach ($filerow in $file1)
    {
    $field1 = $filerow.Computer
    $field2 = $filerow.Description
    $field3 = $filerow.OS
    # Echo "$Field1,$field2,$field3"  <- for debugging
    If ($field3 -like "windows*" -OR $field3 -like "CentOS")
        {
            Echo "$field1,$field2,$field3,Physical" | out-file ".\all_master.txt" -Append
        }
    }
    Foreach ($filerow in $file2)
    {
    $field1 = $filerow.Computer
    $field2 = $filerow.Description
    $field3 = $filerow.OS
    # Echo "$Field1,$field2,$field3" <- for debugging
    If ($field3 -like "windows*" -OR $field3 -like "CentOS")
        { 
            Echo "$field1,$field2,$field3,Virtual" | out-file ".\all_master.txt" -Append
        }
    }
       
    
    #Converts the resultant text file to a CSV
    import-csv ".\all_master.txt" | export-csv ".\all_master.csv" -NoTypeInformation

# This subsection pulls out all servers with Windows.

Echo "Computer,Description,OS,Origin" | Out-file ".\Win_master.txt"

Foreach ($filerow in $file1)
    {
    $field1 = $filerow.Computer
    $field2 = $filerow.Description
    $field3 = $filerow.OS
    If ($field3 -like "windows*")
        {
            Echo "$field1,$field2,$field3,Physical" | out-file ".\win_master.txt" -Append
        }
    }
    Foreach ($filerow in $file2)
    {
    $field1 = $filerow.Computer
    $field2 = $filerow.Description
    $field3 = $filerow.OS
    If ($field3 -like "windows*")
        { 
            Echo "$field1,$field2,$field3,Virtual" | out-file ".\win_master.txt" -Append
        }
    }
       
    
    #Converts the resultant text file to a CSV
    Import-Csv ".\win_master.txt" | export-csv ".\win_master.csv" -NoTypeInformation


# compares the "system" csv's to the "Master" and outputs those to a file for each system.

$file3 = import-csv -Path ".\all_master.csv"
$file4 = import-csv -Path ".\win_master.csv"

compare-object $file4 $file5 -property "Computer" | out-file .\EPO_result.csv
compare-object $file4 $file6 -property "Computer" | out-file .\WSUS_result.csv
compare-object $file3 $file7 -property "Computer" | out-file .\SN_result.csv
compare-object $file4 $file8 -Property "Computer" | out-file .\AD_result.csv

# reformats the results of each file that has been generated by this script.

#AD_result.csv
(Get-Content ".\AD_result.csv") | 
Foreach-Object {$_ -replace "=>", "In AD and not in Master Inventory"} |
Foreach-object {$_ -replace "<=", "In Master Inventory and not in AD"} |
Set-Content ".\AD_result.csv"

#EPO_result.csv
(Get-Content ".\EPO_result.csv") | 
Foreach-Object {$_ -replace "=>", "In EPO and not in Master Inventory"} |
Foreach-object {$_ -replace "<=", "In Master Inventory and not in EPO"} |
Set-Content ".\EPO_result.csv"

#SN_result.csv
(Get-Content ".\SN_result.csv") | 
Foreach-Object {$_ -replace "=>", "In Service Now and not in Master Inventory"} |
Foreach-object {$_ -replace "<=", "In Master inventory and not in Service Now"} |
Set-Content ".\SN_result.csv"

#WSUS_result.csv
(Get-Content ".\WSUS_result.csv") | 
Foreach-Object {$_ -replace "=>", "In WSUS and not in Master Inventory"} |
Foreach-object {$_ -replace "<=", "In Master inventory and not in WSUS"} |
Set-Content ".\WSUS_result.csv"


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>