I'm trying to get our some Linux distros from vCenter by using PowerCLI. But I don't want to get Appliance VMs. So I have 2 different successful PowerCLI scripts those can find these machines. I want merge these scripts but I'm new on PowerCLI and it's syntax.
I'm sharing these scripts at below:
Non-Appliance List:
Get-VM | `
Get-Annotation | `
Where-Object {$_.name -eq "Appliance"} | `
Where-Object {$_.value -eq 'No'} | `
Export-Csv C:\Users\me\Documents\non-appliance-list.csv -NoTypeInformation -UseCulture
Linux List:
Get-View -Property #("Name", "Config.GuestFullName","Guest.GuestFullName") | `
Select -Property Name, #{N="COS";E={$_.Config.GuestFullName}}, #{N="ROS";E={$_.Guest.GuestFullName}} | `
Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `
Select AnnotatedEntity,Name,Value | `
Export-Csv C:\Users\me\Documents\linux-list.csv -NoTypeInformation -UseCulture
Script I imagined but doesn't worked:
Get-VM | `
Get-Annotation | `
Where-Object {$_.name -eq "Appliance"} | `
Where-Object {$_.value -eq 'No'} | `
Get-View -Property #("Name", "Config.GuestFullName","Guest.GuestFullName") | `
Select -Property Name, #{N="COS";E={$_.Config.GuestFullName}}, #{N="ROS";E={$_.Guest.GuestFullName}} | `
Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `
Select AnnotatedEntity,Name,Value | `
Export-Csv C:\Users\me\Documents\linux--list.csv -NoTypeInformation -UseCulture
Maybe It has been a XY-Question. If you have a better way to get Linux VMs those are not appliance, you can say me this method.
You might be better off making use of some variables along the way to help make this a bit easier.
Example:
$LinuxVMs = Get-VM | `
Get-Annotation | `
Where-Object {$_.name -eq "Appliance"} | `
Where-Object {$_.value -eq 'No'}
Now you have the ability to pipeline the LinuxVMs variable into the Export-Csv cmdlet if you need as well as reference it for your second script.
Example:
$LinuxVMs | Get-View -Property #("Name", "Config.GuestFullName","Guest.GuestFullName") | `
Select -Property Name, #{N="COS";E={$_.Config.GuestFullName}}, #{N="ROS";E={$_.Guest.GuestFullName}} | `
Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `
Select AnnotatedEntity,Name,Value | `
Export-Csv C:\Users\me\Documents\linux-list.csv -NoTypeInformation -UseCulture
You have just mashed the scripts together, piping the first into the second.
This won't work.
You can have each script block in a single script, then merge the resulting csv's using one of the methods here:
Merging multiple CSV files into one using PowerShell
Stinkyfriend's code:
Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\merged\merged.csv -NoTypeInformation -Append
Related
I got the current script (from this answer).
This is the script:
`
Get-NetFirewallRule -Action Allow -Enabled True -Direction Inbound |
Where-Object {
$portFilter = $PSItem | Get-NetFirewallPortFilter | Select-Object -Unique
$portFilter.LocalPort -match '^(80|135|139|445|5985|5986)$' -or
($portFilter.LocalPort -ge 49152 -and $portFilter.LocalPort -le 65535)} |
Format-Table Name,Profile,
Enabled,
Direction,
Action,
#{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}},
#{ Name='LocalPort'; Expression={$portFilter.LocalPort | Select-Object -Unique}},
#{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}}
`
How can I implement the current script in AsBuiltReport framework? Is there an easy way to show the result in a report?
I will trigger the script remotely to multiple hosts.
Thanks
Get-ADComputer -Filter 'enabled -eq "true"' `
-Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address |
Sort-Object -Property Operatingsystem |
Select-Object -Property Name,Operatingsystem,OperatingSystemVersion,IPv4Address
I believe somehow combining the script above and below using a for loop.
Get-ItemProperty ` -Path hklm:\System\CurrentControlSet\Control\SecureBoot\State ` | Select-Object UEFISecureBootEnabled
In time I'd like to "assemble" other commands to extract reports/data. I'd like help to learn and create a script I can modify with other commands.
I have a powershell script which is working as expected. I need some help with formatting the output.
$Date = (Get-Date).AddDays(-1)
Get-ChildItem –Path "D:\Log\" -Recurse | Where-Object {($_.LastWriteTime -lt $Date)} | Remove-Item
$filter = #{
LogName='Application'
StartTime=$Date
}
Get-WinEvent -FilterHashtable $filter | Select-Object TimeCreated,Message |
Where-Object { $_.Message -like '*renamed*' -and $_.Message -notlike "*csv*" } |
Out-File -FilePath D:\Log\DailyReport_$(get-date -Format yyyyddmm_hhmmtt).txt
The output is
TimeCreated Message
----------- -------
4/16/2020 4:03:30 AM 04712: renamed
I need the output to be
Date Time,File Name
4/16/2020 4:03:30 AM, 04712: renamed
The column header needs to be renamed with a comma. Any help will be greatly appreciated.
Thanks,
Arnab
You can rename the properties with Select-Object and convert to comma-separated values with ConvertTo-Csv or Export-Csv:
# ...
Get-WinEvent -FilterHashtable $filter |
Select-Object TimeCreated,Message |
Where-Object { $_.Message -like '*renamed*' -and $_.Message -notlike "*csv*" } |
Select-Object #{Name='Date Time';Expression='TimeCreated'},#{Name='File Name';Expression='Message'} |
Export-Csv D:\Log\DailyReport_$(get-date -Format yyyyddmm_hhmmtt).txt -NoTypeInformation
I have some data I am trying to push out to a CSV but it keeps failing. Any advice?
Import-Module ActiveDirectory
$ExportFile = "C:\T2\Test.csv"
$MacHeading = "Mac OS: "
$MACOS = Get-ADComputer -Filter 'OperatingSystem -like "MAC*"' -Properties OperatingSystem, CanonicalName | Select Name, CanonicalName, OperatingSystem
$MACOSCount = Get-ADComputer -Filter 'OperatingSystem -like "MAC*"' | Measure-Object | %{$_.Count}
$MacHeading | Export-CSV -path $ExportFile -Append
$MACOS | Export-CSV -path $ExportFile -Append
$MACOSCount | Export-CSV -path $ExportFile -Append
My error message is:
Export-CSV : Cannot append CSV content to the following file:
C:\T2\Test.csv. The appended object does not have a property that
corresponds to the following column: Mac OS: . To continue with
mismatched properties, add the -Force parameter, and then retry the
command. At C:\T2\Test.ps1:9 char:15
+ $MacHeading | Export-CSV -path $ExportFile -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Mac OS: :String) [Export-Csv], InvalidOperationException
+ FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
while you can create a csv file manually it is usually cumbersome and you will have to match the column headers when you add data manually.
In your code above you start your csv by creating a csv file with one header\column 'mac os'. macos is an objects with various properties and it does not contain a header called 'mac os' so export-csv does not know where to send the data.
also you are missing the -notypeinformation switch to export-csv without which the csv will contain an additional unneeded header with object type
you can look at doing something like this:
$comps = Get-ADComputer -Filter 'OperatingSystem -like "MAC*"' -Properties OperatingSystem, CanonicalName |
Select-Object #{N='MacHeading';e={'Mac OS'}},Name, CanonicalName, OperatingSystem
$comps |
ForEach-Object -Begin {$i = 0} -Process {$I++; $_ | Add-Member -Name ID -Value $i -MemberType NoteProperty -PassThru} |
Export-Csv -Path $path -NoTypeInformation
$comps=Get-ADComputer -Filter { OperatingSystem -Like '*MAC*' } -Properties OperatingSystem,CanonicalName
If you're willing to go without the $MacHeading (which isn't terribly useful for a CSV), you could just do this:
Get-ADComputer -Filter 'OperatingSystem -like "MAC*"' -Properties OperatingSystem, CanonicalName |
Select Name, CanonicalName, OperatingSystem |
Export-CSV -path $ExportFile -Append -NoTypeInformation
Get-ADComputer -Filter 'OperatingSystem -like "MAC*"' |
Measure-Object | %{$_.Count} |
Out-File $ExportFile -Append -Encoding ASCII
The key is to use Out-File instead of Out-CSV for the items that won't really make a CSV.
I am trying to get few items from registry by using where-object, I can only have one item filtered but multiple items...is there anything wrong with my script?
This code works fine for only one item
Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ |
ForEach-Object {Get-ItemProperty $_.pspath} |
Where-Object {
$_.Displayname -like 'adobe air'
} |
Select-Object DisplayName,DisplayVersion |
Sort-Object DisplayName |
Out-GridView
But if i set it to filter multiple items, it runs, ends, without any result... any idea why?
Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ |
ForEach-Object {Get-ItemProperty $_.pspath} |
Where-Object {
$_.Displayname -like 'adobe air' -and
$_.Displayname -like 'Java*' -and
$_.Displayname -like 'TeamViewer*'
} |
Select-Object DisplayName,DisplayVersion |
Sort-Object DisplayName |
Out-GridView
If I use
Where-Object {
$_.Displayname -like "Security*"
}
It only gives me 3 items matches Security, not all of them, Why?
The issue is the logic you put in. -and means both need to be true, use -or instead.