How to implement PowerShell code in AsBuiltReport - powershell

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

Related

when exporting to the text file compare-object issue

I want to get an output like below.
My output now :
Name Active PrimarySmtpAddress
---- ------ ------------------
DG_Group1 True mail1#contoso.com
DG_Group2 False mail2#contoso.com
DG_Group3 True mail3#contoso.com
My desired output :
mail1#contoso.com
mail2#contoso.com
mail3#contoso.com
script :
$DistroLists = Get-DistributionGroup -ResultSize Unlimited
$MessageTrace = Get-MessageTrace -RecipientAddress $DistroLists.PrimarySmtpAddress -startdate (Get-Date).AddDays(-8) -EndDate (Get-Date)
$DistroLists |
Foreach-Object {
$_ | Add-Member -MemberType NoteProperty -Name Active -Value (
$_.PrimarySmtpAddress -in $MessageTrace.RecipientAddress
) -PassThru
} |
Select-Object Name, Active, PrimarySmtpAddress | Where-Object Active -EQ "FALSE"
Out-File C:\output.txt
You can use Select-Object -ExpandProperty or ForEach-Object -MemberName to grab only the value of a specific property from one or more piped input objects:
... |Select-Object Name, Active, PrimarySmtpAddress | Where-Object Active -eq $false | ForEach-Object -MemberName PrimarySmtpAddress | Out-File...
You can skip first two lines and split the line using space characters. Finally pick the last column as given below:
Get-Content C:\Projects\logtext.txt | Select-Object -Skip 2 | ForEach-Object { "$(($_
-split '\s+',3)[2])" }
mail1#contoso.com
mail2#contoso.com
mail3#contoso.com

PowerCLI Get VMs those fit some conditions

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

Want to put IP address v4 of my specifically named adapter into a variable using Powershell

I have several network adapters on my PC.
I want to simply get the IP Address v4 (with no headers or extras) of the adapter called a specific name and store it into a variable for further use.
I am able to return the IP Address with headers but not what I want.
Please Help - thanks.
Here is what I tried:
$ipa = Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } |
Select-Object { $_.IPAddress }
Edit:
$ipa = Get-NetIPAddress | where {$_.InterfaceAlias -eq "LAN2" -and
$_.AddressFamily -eq "IPv4"} | select -expandproperty ipaddress
The above code is returning both my wired adaptors' addresses but it is returning just the Ip address at least and nothing else (thanks Anthony)
There is ONLY one called "LAN2" - i only need that one - so still stuck
Update :
Austin Frenchs second solution works great - will test Owls today later - thanks everyone so far - great helpful community
simple one liner
(Get-NetIPConfiguration).ipv4address | where {$_.InterfaceAlias -eq "Lan2"} | foreach { write-host $_.ipaddress}
get from enabled network
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | select Ipaddress
only from particular adapter and ipv4
Get-WMIObject win32_NetworkAdapterConfiguration |
Where-Object { $_.IPEnabled -eq $true } |
Foreach-Object { $_.IPAddress } |
Foreach-Object { [IPAddress]$_ } |
Where-Object { $_.AddressFamily -eq 'LAN2' } |
Foreach-Object { $_.IPAddressToString }
Without access to get-netipaddress, here is a very dirty and long one liner. Change out "Local Area Connection" for your network's name.
((([System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? {$_.Name -eq "Local Area Connection"}).GetIPProperties() | Select-Object -ExpandProperty 'UniCastAddresses' | ? {$_.IPv4Mask -ne
"0.0.0.0"}) | Select Address).Address.IPAddressToString
With Get-netIpAddress, I suspect this would also work:
$ipa = (Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } | `
Select-Object { $_.IPAddress }).IPAddress
You can accomplish what you are looking for by first getting a list of all network adapters on the machine using get-wmiobject, selecting the name you want, then piping it to get-netipaddress and then selecting the IPAddress property as shown below.
(Get-WmiObject win32_networkadapter | Where-Object {$_.name -eq "YOUR_NAME"} | Get-NetIPAddress).IPAddress

How to filter multiple items from regerstry by using whrere-object -like?

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.

List all Server Features/Roles in Powershell

So I have the following code to output all features and roles installed:
Import-Module ServerManager
$Arr = Get-WindowsFeature | Where-Object {$_.Installed -match “True”} | Select-Object -Property Name
$loopCount = $Arr.Count
For($i=0; $i -le $loopCount; $i++) {
Write-Host $Arr[$i]
}
However, the output is:
#{Name=Backup-Features}
#{Name=Backup}
#{Name=Backup-Tools}
How can I get rid of the # and {}'s ?
Use Select -ExpandProperty Name instead of Select -Property Name
Alternatively and also, I recommend using Foreach-Object instead of a C-style for loop.
Import-Module ServerManager
Get-WindowsFeature |
Where-Object {$_.Installed -match “True”} |
Select-Object -ExpandProperty Name |
Write-Host
Or
Import-Module ServerManager
Get-WindowsFeature |
Where-Object {$_.Installed -match “True”} |
ForEach-Object {
$_.Name | Write-Host
}
How about a nice one liner?
Get-WindowsFeature | ? {$_.Installed -match “True”} | Select -exp Name
If you can accept a totally static solution, this should work:
Write-Host $Arr[$i].Substring(2, $Arr[$i].Length-3)
If you're looking for a solution that looks specifically for those symbols and removes them, it would be a little different. Based on your question though, this should be just fine.