Azure PowerShell cmdlet into Strings - powershell

Just a quick question. I am running lines of code like,
$publicIP = Get-Content (Get-AzureVM -ServiceName $servicename -Name $vmsname | Get-AzureEndpoint | Select { $._vip })
$OSDisk = Get-Content (Get-AzureVM -ServiceName $servicename -Name $vmsname | Get-AzureOSDisk)
And it does get me the IPAddress or the variables, but when trying to put the value into a csv cell, it fails. I think it is because it tries to add the extra labels, but I do not know how to get just the string and set the variable as just that. Does anyone know how to remedy this?
edit:
If the output with:
Get-AzureVM -servicename "vm1" -name "vm1" | Select DNSName | Out-String
is like this:
DNSName
-------
http://example.cloudapp.net/
How do I just put in "http://example.cloudapp.net/" as a CSV-entry? Right now it is trying to put all of the code block into the CSV which is giving me awful formatting erros.

Select is an alias for Select-Object and allows you to take one or more properties from an object and creates a new object with them.
If you want to expand a property into just the value, use the -ExpandProperty parameter:
Get-AzureVM -servicename "vm1" -name "vm1" | Select -ExpandProperty DNSName
Should give you just http://example.cloudapp.net/
Also, when you want to send data to a CSV, you can pipe objects to the cmdlet Export-CSV which will use the property names as headers and the objects in the array as rows and the properties as columns.
Example:
Get-ChildItem -File | Select Name, Size | Export-Csv c:\temp\filelist.csv

Related

Get-Printer cmdlet applied to multiple remote servers

I'm trying to run the cmdlet "Get-Printer" and apply it to multiple computers but I get an error message I tried using double quotations marks and get a same error message.
"Get-Printer : Cannot process argument transformation on parameter 'ComputerName'. Cannot convert value to type System.String."
Get-Printer -ComputerName 'server01', 'server02'|select-object -Property Name, PortName |Select-String -Pattern 127.0.0.1
is it because get-printer cmdlet can only be applied to one server at a time? do I have to take a different approach to be able to apply it to multiple servers?
ComputerName is a string not an array. See the documentation for Get-Printer.
You can however use another pipe (|) at the beginning followed by a block beginning with a % (shorthand for ForEach-Object) in order to feed the strings to the cmdlet one at a time. It would look something like this:
'server01', 'server02' |
%{Get-Printer -ComputerName $_} |
Select-Object -Property Name, PortName |
Select-String -Pattern 127.0.0.1
There is a second issue with this though. Select-String also expects a string, but you're passing it an object containing Name and PortName. Instead of using Select-String, you could probably just pipe it to a block prefixed by ? (shorthand for Where-Object) and check if PortName contains 127.0.0.1.
Adding this would look like:
'server01', 'server02' |
%{Get-Printer -ComputerName $_} |
Select-Object -Property Name, PortName |
?{$_.PortName.Contains("127.0.0.1")}
If you would like, you can also shorten Select-Object to just Select, but I'll leave that up to you.
The help of Get-Printer shows that the parameter -ComputerName accepts input of type string, but not string array. So you can only specify a single computer name.
https://learn.microsoft.com/en-us/powershell/module/printmanagement/get-printer?view=windowsserver2019-ps
What you can do however, is to define the array of computer names first and then use a foreach.
$computers = "server01","server02"
foreach($computer in $computers)
{
Get-Printer -ComputerName $computer |select-object -Property Name, PortName | Select-String -Pattern 127.0.0.1
}
Or like Jesse suggested above a one liner would look like this:
"server01","server02" | ForEach-Object {Get-Printer -ComputerName $_} | Select-Object -Property Name, PortName | Select-String -Pattern 127.0.0.1

trim output in powershell or cut a specific amount of letters

in many scripts i have the problem, that it always shows me the complete name like domain\user.
for example like that
Invoke-command -computer computername {Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -Name LastLoggedOnUser | select -ExpandProperty LastLoggedOnUser}
how can i cut off the domain and just have the user name?
i have tried that for example.
Invoke-command -computer computername{Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -Name LastLoggedOnUser | select -ExpandProperty LastLoggedOnUser #{N="LastLoggedOnUser";E={$_.LastLoggedOnUser -replace ".+\"}}}
You can go with :
Invoke-command -computer computername {Get-ItemProperty `
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI `
-Name LastLoggedOnUser | select -ExpandProperty LastLoggedOnUser} | `
%{ $_ -replace "^[^\\]*\\","" }
Backslash is a special regex character that would need to be escaped to be taken literally.
'stackoverflow\joe' -replace '.*\\'
joe
here is yet another way to get the no-domain-name last logged on user from that registry key [grin] ...
what it does ...
sets the reg key to use
sets the prop of that key to get the value from
grabs that value
splits on the \
takes the last item from that split result
assigns it to a $Var
displays the value therein
the code ...
$LogonUiRegKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$TargetPropName = 'LastLoggedOnUser'
$LastLoggedOnUser = (Get-ItemPropertyValue -Path $RegKey -Name $TargetPropName).Split('\')[-1]
$LastLoggedOnUser
output = MyUserName

Get multiple fields back?

Is it possible to get multiple properties back for a command within parentheses? Consider the following code.
$service = (get-service -name 'wuauserv')
$service.Name # I get the name property back
But what If I wanted to get more than one property. For example below:
$service.Name,Status
Why doesn't this work? Is there a way to do it?
What you are dealing with is the concept of how PowerShell handles outputting information and the pipeline. When you collect Get-Service into the variable $service, you are storing an object that has multiple properties.
To work with the properties of a given object (one or many) you utilize Select-Object (docs). Whether you are dealing with that variable contents or directly with the output from Get-Service you have to pipe the output to Select-Object to retrieve one to many properties. You can do this multiple ways. PowerShell works on positions when it comes to parameters, so the position 0 parameter for Select-Object is -Property. All of the following are equivalent, and just various ways to get the same result:
$proc = Get-Service -Name mssql*
$proc | Select-Object Name, DisplayName
$proc = Get-Service -Name mssql*
Select-Object -Property Name, DisplayName -InputObject $proc
Get-Service -Name mssql* | Select-Object Name, DisplayName
If you want your variable to only contain a given set of properties then you would also utilize Select-Object:
$proc = Get-Service -Name mssql* | Select-Object Name, DisplayName
$proc
Output Example:
When you do $Service.Name it returns a the expanded property, how would you return the expanded property for multiple properties?
PS H:\> $Service = (Get-Service -name 'wuauserv')
PS H:\> $Service | Select-Object Name, Status
Name Status
---- ------
wuauserv Stopped
Notice how they are not an expanded property.
PS H:\> $service.Name
wuauserv
PS H:\> $service | Select-Object -ExpandProperty Name
wuauserv
Trying to expand multiple properties will end up with an error as the method cannot accept multiple arguments (Name, Status).

Getting format-table to work correctly with multiple sources

I want to check for specific services for a few specific servers and want the output to show on the same formatted table. I've only been able to create multiple tables, or to show only the last table formatted the way I want to. My intention is to show all on the same table.
Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 |
Format-Table -Property MachineName, Status, Name, DisplayName -Auto
How do I include SERVER1 and SERVER2 on the same formatted table? The example above will only show me formatted table for the SERVER2?
Other way I've tried was to
Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1 |
Format-Table -Property MachineName, Status, Name, DisplayName -Auto
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 |
Format-Table -Property MachineName, Status, Name, DisplayName -Auto
but that way there's two different tables created, and not all the info in only one like i would like to.
I need to check differente services on six different servers, but with just two I think is enough to exemplify my difficulties on this script.
If you want them both to appear in one table, you need to send all of the results over to Format-Table at the same time. If you call Format-Table twice, you're going to get two separate tables.
Fortunately, PowerShell makes it really easy to store the results of a command in a variable, and then use that later.
What you need to do is make a variable to hold the results, and then store all of your Get-Service commands inside of it, like so:
#take the output and store it in $services
$services = get-service bits,winrm -computername ServerA
#add the output of the next to $services as well
$services += get-service AdobeARMservice,ALG -computername ServerB
#finally, make one big table to display it all
$services |Format-Table -Property MachineName, Status, Name, DisplayName -auto
MachineName Status Name DisplayName
----------- ------ ---- -----------
ServerA Running bits Background Intelligent Transfer Service
ServerA Running winrm Windows Remote Management (WS-Management)
ServerB Running AdobeARMservice Adobe Acrobat Update Service
ServerB Stopped ALG Application Layer Gateway Service
Before you go too far down this rabbit hole, keep in mind that Format-Table is ONLY meant for viewing stuff in the console. You can't take a table made with FT and then send export it as a .csv, for instance. If you're OK with just viewing it in the console though, this should work.
You could do it like this:
# create array
$services = #()
# add items to array
$services += Get-Service spooler
$services += Get-Service wsearch
# format array
$services | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize
Or like this:
# create list
$services = New-Object System.Collections.ArrayList
# add items to list
$services.Add($(Get-Service spooler)) | Out-Null
$services.Add($(Get-Service wsearch)) | Out-Null
# display list
$services | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize
Instead of populating an array or ArrayList, you can simply use a sub-expression to collate both outputs to a single Format-Table:
$(
get-service "ServiceA", "ServiceB", "ServiceC" -computername SERVER1
get-service "ServiceD", "ServiceE", "ServiceF" -computername SERVER2
) | Format-Table -Property MachineName, Status, Name, DisplayName -auto
Your examples don't produce the desired results, because for the first example only the output of the second Get-Service goes into Format-Table, and for the second example two separate tables are created.
If you take a look at the documentation of the Get-Service cmdlet you'll notice that both the -Name and the -ComputerName take a string array as input, so if you want to check the same services on all computers you can simply do something like this:
$servers = 'SERVER1', 'SERVER2'
$services = 'ServiceA', 'ServiceB', 'ServiceC'
Get-Service $services -ComputerName $servers |
Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize
If you want to check different services on each server I'd map the services to the servers with a hashtable
$services = #{
'SERVER1' = 'ServiceA', 'ServiceB', 'ServiceC'
'SERVER2' = 'ServiceD', 'ServiceE', 'ServiceF'
}
and run Get-Service in a ForEach-Object loop, like this:
$services.Keys | ForEach-Object {
Get-Service $services[$_] -ComputerName $_
} | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize
or like this:
$services.GetEnumerator() | ForEach-Object {
Get-Service $_.Value -ComputerName $_.Name
} | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize

Get-Service results to array

I'm sure this should be straight forward but I've been stuck on it for a while now...
I am trying to get the service names (for sql server) into an array but can't figure out how to do it. I basically want the array contents to look something like the output of this:
Get-Service -computername $server_name -name sql* | format-table -property name
I have tried things like this but the contents of $service_name are very odd:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter | format-table -property name)
Whatever I try either errors or gives some strange message in my array. Should this be easy/possible? I'm guessing I could dump the results in a text file then use the contents of that but it's a bit messy and more overhead than seems necessary.
Not sure, but where you looking for this (with whatever variation on the pattern for -Name and/or -Exclude)?
PS> $names = (Get-Service -Name Sql* | select name)
PS> $names.GetType().IsArray
True
PS> $names.Length
3
PS> $names
Name
----
SQLBrowser
SQLSERVERAGENT
SQLWriter
Or even along the lines of the following if you really want the "Name" as a System.String array.
PS> $names = (Get-Service -Name Sql* | foreach { $_.Name -as [string]})
PS> $names[0].GetType().FullName
System.String
PS> $names
SQLBrowser
SQLSERVERAGENT
SQLWriter
But also keep the good advice in #alroc's answer in mind - maybe you want to keep the actual type of Get-Service's result (System.ServiceProcess.ServiceController) as long as possible and access/use the Name property of it as late as possible. YMMV.
You're using format-table in the pipeline. Anytime you use a format-* cmdlet, that's the end of the line for your data - it's now just a formatted bunch of text, it's no longer data that you can actually use.
Try this to get the names formatted as a table for viewing:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter);
$service_name | format-table -property name;
That said, I ran both your version and mine above and got the same visual output - the critical difference is that mine stores data in $service_name, not just a bunch of text (as yours does).
edit:
In response to:
All I really needed was the service names in an array to then use elsewhere
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter) | select-object -expandproperty name;
or:
$service_name = (Get-Service -computername $server_name -name sql* -exclude *sqlwriter).name;
No need to invoke WMI directly.