How to Combining PowerShell Output from multiple server to Single csv - azure

I need to pull the report from below code, now I'm running this in every server separately.
Is it possible to combine the the all output from different server and get it in one csv file or get it to my email directedly.
where to define the servers name ?
how to display the server name in every output ?
how to combine and get in single output ?
Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Import"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, Result
Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Synchronization"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, Result
Get-ADSyncToolsRunStepHistory | Where {$_.RunProfileName -match "Full Import"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, StepResult
Get-ADSyncToolsRunStepHistory | Where {$_.RunProfileName -match "Full Synchronization"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, StepResult

you could do this from local, to start with:
in servers.txt file - add your servers hostname line by line like
server1
server2
$servers = Get-Content "c:\temp\servers.txt"
$Report = foreach ($server in $servers){
Invoke-Command -ComputerName $server -ScriptBlock{
Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Import"} | Select-Object #{N='ServerName';E={"$env:COMPUTERNAME"}},StartDate, EndDate, ConnectorName, RunProfileName, Result
}
}
$Report|Export-Csv "c:\temp\DeltaImport.csv" -NoTypeInformation
Before Using Invoke-Command, I suggest you to go through this link once:
https://4sysops.com/archives/use-powershell-invoke-command-to-run-scripts-on-remote-computers/

Related

Powershell - cut domain name from FQDN

I would like to list all Session Hosts in all Collections, convert to string. I get this with:
$SHs = (Get-RDSessionCollection -ConnectionBroker $CB | Select-Object -Property collectionname | ForEach-Object -Process { Get-RDSessionHost -ConnectionBroker $CB -CollectionName $_.collectionname }) |select SessionHost |Out-String
Output:
SessionHost
-----------
SH1.contoso.com
SH2.contoso.com
SH3.contoso.com
Now I would like cut domain name .contoso.com from fqdn.
foreach ($SH in $SHs){
$SH = $SH.Substring(0,$SH.Length-12)
$SH
}
but the output still:
SessionHost
-----------
SH1.contoso.com
SH2.contoso.com
SH3.contoso.com
Desired Output is:
SH1
SH2
SH3
Edit: I tried followings codes (thx for help):
$SH = ($SH -split "\.")[0]
and
$SH = ($SH -split '\.' | Select -first 1) -join '.'
and I receive only :
SH01
Thank you for any help!
Without having a terminal server to test this should work actually:
$SHs = (
Get-RDSessionCollection -ConnectionBroker $CB |
Select-Object -Property collectionname |
ForEach-Object -Process {
Get-RDSessionHost -ConnectionBroker $CB -CollectionName $_.collectionname
}
) |
Select-Object -Property SessionHost
foreach ($SH in $SHs.SessionHost) {
($SH -split "\.")[0]
}

Powershell, how obtain a csv by column with a pscustomobject or ordered?

Being a beginner in Powershell, I have a problem that I can't solve. I get the average of different CSV files as a string with the following code:
# recovery of the list of csv files present in the folder "folder_1".
$files=(Get-ChildItem -path "C:\folder_1\" -Recurse -Include *.csv)
#loop to read each file and average the variables "PRESS_CELL" and "PRESS_DELTA
foreach($file in $files){
$varTemp = #((Get-Content $file | ConvertFrom-Csv | Measure-Object "PRESS_CELL","PRESS_DELTA" -Average | Export-Csv -Path "C:\folder_1\Alias.csv" -NoTypeInformation -Append))
Write-Host $varTemp
}
#added an increment to sort the data (averages)
$vartemp2 = Import-CSV "C:\folder_1\Alias.csv" | Select *,LINENUMBER | ForEach-Object -Begin { $Line = 1 } {
$_.LineNumber = $Line++
$_
} | Export-CSV "C:\folder_1\Alias2.csv" -NoTypeInformation
Write-Host $vartemp2
# sort data by "property" and by "LINENUMBER"
$vartemp3 = Import-Csv "C:\folder_1\Alias2.csv"
$vartemp3 | % { $_.LINENUMBER = [int]$_.LINENUMBER }
$vartemp3 | Sort-Object -Property #{ Expression = 'Property'; Ascending = $true }, #{ Expression = 'LINENUMBER'; Ascending = $true } |
Format-Table -Property "Average","Property","LINENUMBER"
# groups the values obtained in a table/customobject and export it to csv by column ?
$fields = [ordered]#{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average
}
$fields | export-csv C:\1_ICOS_data\Alias4.csv -Append ##DOESN'T WORK
[result][1]
However, I would like to export the result as a CSV table (1 column per result with PRESS_CELL and PRESS_DELTA as headers. When I use the pipeline :
| export-csv C:\temp\alias.csv -Append
I get a result :
With "ordered"
"System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection","System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection","False","System.Object","False"
And with pscustomobject :
System.Object[],"System.Object[]"
Add -NoTypeInformation to export-csv command like that
$fields = [ordered]#{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average
}
#Or:
$fields = [pscustomobject]#{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average }
$fields |export-csv "c:\fields.csv" -Append -NoTypeInformation

Export CSV Writes Results to Single Line, How Do I get Multiple Lines?

I am looking for duplicates on a share drive so I can let the users know and they can clean it up before we use anything automated. My largest duplicate is close to 400 copies, but the info is all on a single line.
My query is getting the correct results:
$a = Get-ChildItem -Path "S:\" -File -Recurse |
Select-Object -Property Fullname, #{N='Hash';E={(Get-FileHash $_.FullName).Hash}}
$cnt = $a | Group-Object -Property Hash
$cnt |
Select-Object Count, #{N='FullName';E={($_.Group).FullName}}, #{N='Hash';E={($_.Group).Hash}} |
Sort-Object -Property Count -Descending |
Export-Csv C:\Temp\S_Drive_Counts.csv
Here is an example of my results where each entry is on a single line:
"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83 498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
I need to pass this info on though, so I'd like to have each entry on a line by itself, similar to this:
"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
"2","S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
I can do some string manipulation to the CSV if needed, but I am looking for a way to get it in the correct format before exporting to the CSV.
Unroll your groups. Also, make better use of the pipeline.
Get-ChildItem -Path 'S:\' -File -Recurse |
Select-Object Fullname, #{n='Hash';e={(Get-FileHash $_.FullName).Hash}} |
Group-Object Hash |
ForEach-Object {
$cnt = $_.Count
$_.Group | Select-Object #{n='Count';e={$cnt}}, FullName, Hash
} |
Sort-Object Count, Hash, FullName -Descending |
Export-Csv 'C:\Temp\S_Drive_Counts.csv' -NoType

Output server information to Excel file

I've a PowerShell script that can generate server status info to me. My problem now is I want to output result to an Excel file.
PowerShell code:
function getwmiinfo ($svr) {
gwmi -Query "select * from Win32_ComputerSystem" -ComputerName $svr |
select Name, Model, Manufacturer, Description, DNSHostName, Domain,
DomainRole, PartOfDomain, NumberOfProcessors, SystemType,
TotalPhysicalMemory, UserName, Workgroup |
Format-Table -Property * -Autosize | Out-String -Width 10000
gwmi -Query "select * from Win32_OperatingSystem" -ComputerName $svr |
select Name, Version, FreePhysicalMemory, OSLanguage, OSProductSuite,
OSType, ServicePackMajorVersion, ServicePackMinorVersion |
Format-Table -Property * -Autosize | Out-String -Width 10000
gwmi -Query "select * from Win32_PhysicalMemory" -ComputerName $svr |
select Name, Capacity, DeviceLocator, Tag |
Format-Table -Autosize
gwmi -Query "select * from Win32_LogicalDisk where DriveType=3" -ComputerName $svr |
select Name, FreeSpace, Size |
Format-Table -Autosize
}
$servers = Get-Content 'servers.txt'
foreach ($server in $servers) {
$results = gwmi -query "select StatusCode from Win32_PingStatus where Address = '$server'"
$responds = $false
foreach ($result in $results) {
if ($result.statuscode -eq 0) {
$responds = $true
break
}
}
if ($responds) {
getwmiinfo $server
} else {
Write-Output "$server does not respond"
}
}
Output to Excel:
You can use Export-Csv (Export-Csv file path -NoTypeInformation). This is the command.
If you want to use array then you will have to define that. You can check this question.
E.g. $variable | Export-Csv c:\output.csv -notypeinformtion

How to get a list of SiteCollections with the same name?

I have the following powershell, I am getting all sites ordering by name, but I would like to know if there is anyway to know which sites have the same name>?
$sites = Get-SPSite -Limit All
$sites | where { $web.WebTemplate -eq "Client"}
select Url,$_.Name.ToLower(), $_.RootWeb.Created, $_.RootWeb.AllProperties["ClientCode"], $_.RootWeb.AllProperties["ClientName"],$_.RootWeb.AllProperties["ClientSiteCode"] | format-table -auto | Sort-Object $_Name -descending
You can group the sites by their name and filter the count is greater then 1:
Get-SPSite -Limit All |
Group-Object Name |
Where-Object {$_.Count -gt 1}

Resources