Assistance with writing out this script to an excel file - excel

I have this script that reads in a list of computernames and writes out networking details on each VM (IP, Subnet, Gateway, DNS Servers, MAC). It works in that the script writes out to my powershell window. I'm trying to add in some code to write the output to a .csv file. Here is my script:
$inputFile = "C:\Powershell_Scripts\IP_Mac\servers.txt"
$csvFile = "C:\Powershell_Scripts\IP_Mac\results.csv"
$report = #()
foreach($Computer in (gc -Path $inputFile)){
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
Write-Output $report
#}
#When writing out the data in $node, use the tags defined in $properties to write out to a .csv file
$report | Select-Object -Property $properties | Export-Csv -Path $csvFile -NoTypeInformation
It's something with my last line when I'm writing out the report. Any help you can provide on how to write the results to a file would be greatly appreciated.
Thank you.

You are not adding anything to your $report array, change the last $OutputObj line
...
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress
-Value $MACAddress
$OutputObj
}
...
to
...
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress
-Value $MACAddress
$report += ,$OutputObj
}
...
and last line
$report | Select-Object -Property $properties |
Export-Csv -Path $csvFile -NoTypeInformation
to
$report | Export-Csv -Path $csvFile -NoTypeInformation
To capture details of machines you can't connect to you could change line 8 to:
try{
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration
-ComputerName $Computer | ? {$_.IPEnabled}
}
catch{
Write-Output($Computer + "`t" + $_.Exception.Message ) |
Out-File "C:\Powershell_Scripts\IP_Mac\bad_computers.tsv"
}
and you will get a tsv file with computer name and error message.

Related

Convert Output or Object in Array to Table Powershell

I created a custom object
$customsa = New-Object -TypeName psobject
$customsa | Add-Member -MemberType NoteProperty -Name Name -Value (Get-AzStorageAccount).StorageAccountName
$customsa | Add-Member -MemberType NoteProperty -Name Tier -Value (Get-AzStorageAccount).AccessTier
$customsa | Add-Member -MemberType NoteProperty -Name Replication -Value (Get-AzStorageAccount).sku.Name
$customsa | Add-Member -MemberType NoteProperty -Name AccountKind -Value (Get-AzStorageAccount).kind
$customsa | Add-Member -MemberType NoteProperty -Name ResourceGroupName -Value (Get-AzStorageAccount).ResourceGroupName
The output is an array format
Output
However I want the output to show in a table format like this
Desired Table format
Thanks
Have you tried?:
$customsa | Format-Table
# or
$customsa | Out-GridView
From your example:
$customsa = New-Object -TypeName psobject
$customsa | Add-Member -MemberType NoteProperty -Name Name -Value (Get-AzStorageAccount).StorageAccountName
$customsa | Add-Member -MemberType NoteProperty -Name Tier -Value (Get-AzStorageAccount).AccessTier
$customsa | Add-Member -MemberType NoteProperty -Name Replication -Value (Get-AzStorageAccount).sku.Name
$customsa | Add-Member -MemberType NoteProperty -Name AccountKind -Value (Get-AzStorageAccount).kind
$customsa | Add-Member -MemberType NoteProperty -Name ResourceGroupName -Value (Get-AzStorageAccount).ResourceGroupName
$customsa | Format-Table
Get-AzStorageAccount is returning all of your storage accounts each time you run it so you are assigning the value of all storage accounts to each property when you're using Add-Member.
You should be able to get the same result using Select-Object which will create your psobject for you
Get-AzStorageAccount |
Select-Object StorageAccountName, AccessTier, Kind, ResourceGroupName, #{name = 'SKU'; expression = { $_.sku.name }} |
Format-Table

Splitting Export large CSV files via Powershell

Like everyone know excel has got max rows 1048574. My question is :when the max row is reached, then move to a new CSV file I have tried something no luck I can't figure it out.
$RootFolder = Get-Content "c:\DRIVERS\myfile.txt"
foreach ($arrayOfPaths in $RootFolder){
$csv = $arrayofPaths -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'
Get-ChildItem $arrayOfPaths -Recurse | Where-Object {$_.mode -match "d"} | ForEach-Object {
$path = $_.FullName
Get-Acl $path | Select-Object -Expand Access |
Select-Object #{n='Path';e={$path}}, IdentityReference, AccessControlType,
FileSystemRights |
Export-Csv $csv -Append -NoType
}
}
Last Update :
$RootFolder = Get-Content "c:\DRIVERS\myfile.txt"
foreach ($arrayOfPaths in $RootFolder){
$csv = $arrayofPaths -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'
$csvIndex = 1
$maxRows = 1000000
$rowsLeft = $maxRows
Get-ChildItem $arrayOfPaths -Recurse | Where-Object {$_.mode -match "d"} | ForEach-Object {
#$csv = $_.FullName -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'# <- construct CSV path here
$path = $_.FullName
$thisCSV = Get-Acl $path | Select-Object -Expand Access |
Select-Object #{n='Path';e={$path}}, IdentityReference, AccessControlType,
FileSystemRights |
ConvertTo-Csv
if ($thisCSV.count -lt $rowsLeft) {
$thisCSV | Export-Csv $csv -append -noType
$rowsLeft -= $thisCSV.count
} else {
$thisCSV[0..($rowsLeft - 1)] | Export-Csv $csv -append -noType
$csvIndex++
$csv = $csv -replace '\.csv$', "$csvIndex.csv"
if ($thisCSV.count -gt $rowsLeft) {
$thisCSV[$rowsLeft..($thisCSV.count - 1)] | Export-Csv $csv -append -noType
}
$rowsLeft = $maxRows - ($thisCSV.count - $rowsLeft)
}
}
}
Do ConvertTo-Csv in a temporary variable, add up its .Count property until the split point is reached, then split the CSV array to a new file with a new name that will be used from this point on.
Below is a simplified example that assumes no single CSV can exceed 1000000 rows.
$csv = .............
$csvIndex = 1
$maxRows = 1000000
$rowsLeft = $maxRows
...........
$thisCSV = Select-Object ........ |
ConvertTo-Csv
if ($thisCSV.count -lt $rowsLeft) {
$thisCSV | Export-Csv $csv -append -noType
$rowsLeft -= $thisCSV.count
} else {
$thisCSV[0..($rowsLeft - 1)] | Export-Csv $csv -append -noType
$csvIndex++
$csv = $csv -replace '\.csv$', "$csvIndex.csv"
if ($thisCSV.count -gt $rowsLeft) {
$thisCSV[$rowsLeft..($thisCSV.count - 1)] | Export-Csv $csv -append -noType
}
$rowsLeft = $maxRows - ($thisCSV.count - $rowsLeft)
}

Powershell - Find and Replace

I've written the below:
$Path = "C:\"
$Folders = Get-ChildItem $Path | Where-Object {$_.mode -match "d"}
$FolderName = #()
$Folders | ForEach-Object { $FolderName += $_.name }
ForEach ($Name in $FolderName) {
$File = "C:\$Name\file.xaml"
$Content = Get-Content $File
$A = "ValueA"
$B = "ValueB"
$C = "ValueC"
$D = "ValueD"
$Content | ForEach-Object {$_ -Replace $A, $B} | Set-Content $File
$Content | ForEach-Object {$_ -Replace $C, $D} | Set-Content $File
}
However, the first find and replace does not work. I thought it was because the actual string I'm looking for is quite large, but if I change nothing in the program and then comment out the second find and replace operation, the first one starts to work. Can anyone spot why this is please? Many Thanks!
You need to refresh $Content before doing the second find and replace. At the moment, you're setting the content of the file in the first call to Set-Content, but the variable $Content still contains the old contents of the file. If you call $Content = Get-Content $File again before doing the second call, you should be golden.
Assuming your issue is not that the values overlap eachother your are writing the same source content back to file. You should combine the two Set-Content lines.
$Content | ForEach-Object {$_ -Replace $A, $B -Replace $C, $D} | Set-Content $File

Powershell extracting IIS Bindings

I have an Excel spreadsheet that has about 190 lines worth of IIS bindings. What I need to do is go line by line and get each binding from the line.
Example from one line could be:
SITE "Gateway"
(id:1,bindings:http/192.168.1.1:80:www.sample.test.com,http/192.168.1.1:80:sample.test.com,https/192.168.1.1:443:,net.tcp/808:,net.pipe/,net.msmq/localhost,msmq.formatname/localhost,state:Started)
So I would need to seperate out the two webpages, www.sample.test.com and sample.test.com. Then what I wanted to do was do an nslookup on each webpage that is pulled out from each line.
I have gotten as far as going line by line and getting each set of bindings using:
$worksheet = $workbook.sheets.item($sheetname1)
For ($1=0;$i -le 190;$i++) {
$a = $worksheet.Columns.Item(1).Rows.Item($i).text}
Edit:
I have added the following code and it seems to work (but is there a better way to do this?)
For ($1=0;$i -le 190;$i++) {
$a =#($worksheet.Columns.Item(1).Rows.Item($i).text)
For ($2=0;$2 -lt $a.length; $2++) {
$a[$2].split(':,')[5]
$a[$2].split(':,')[8]
$a[$2].split(':,')[11]
$a[$2].split(':,')[14]
$a[$2].split(':,')[17]
}
I got it to work with this but now my issue is that some of the lines dont have more than two host names to do the nslookup so in the output file I get extra data that I dont want
$a =#($worksheet.Columns.Item(1).Rows.Item($i).text)
For ($2=0;$2 -lt $a.length; $2++) {
$d = $a[$2].split(':,')[5] | Out-File -Filepath $outfile -append
$a[$2].split(':,')[5] | nslookup($_) | Out-File -Filepath $outfile -append
$e = $a[$2].split(':,')[8] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[8] | nslookup($_) | Out-File -Filepath $outfile -append
$f = $a[$2].split(':,')[11] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[11] | nslookup($_) | Out-File -Filepath $outfile -append
$h = $a[$2].split(':,')[14] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[14] | nslookup($_) | Out-File -Filepath $outfile -append
$j = $a[$2].split(':,')[17] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[17] | nslookup($_) | Out-File -Filepath $outfile -append
$k = $a[$2].split(':,')[20] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[20] | nslookup($_) | Out-File -Filepath $outfile -append
$l = $a[$2].split(':,')[23] |Out-File -Filepath $outfile -append
$a[$2].split(':,')[23] | nslookup($_) | Out-File -Filepath $outfile -append}

How do I concatenate a string within a loop with PowerShell?

The command
Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt
writes a list to output.txt where only the column Name will be printed in the form:
a
b
c
...
Now what I want to achieve is to append ,xxx to each line in some sort of loop, so that I get the following:
a,xxx
b,xxx
c,xxx
...
I tried to append the string, but this doesn't seem to work:
Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt | Add-Content output.txt ",xxx"
I'm really not familiar with PowerShell, and I did not find a way to concatenate ,xxx.
In my case it is essential to do the concatenation within a loop, not with a file operation afterwards.
Instead of foreach { $_.Name }, write foreach { "$($_.Name),xxx" }

Resources