Unable to extract VirtualNetwork Name using SCVMM powershell modules - arrays

Im trying to extract virtual network information for a VM using powershell, i tried using regular expression but for VM's with more than 1 NIC im unable to see output
Below is the output which i need..
PS C:\> get-vm sql.IAN01.Host | select -ExpandProperty virtualnetworkadapters | select virtualnetwork
VirtualNetwork
--------------
VirtualUplink
iSCSI1
iSCSI2
VirtualUplink
But when i try using regular expressions it does not give me an output, Network comes blank
PS C:\> Get-VM sql.IAN01.Host | Select #{Name="VMName";Expression={$_.name}},#{Name="Network
";Expression={#((get-vm $_.name | select -ExpandProperty virtualnetworkadapters).virtualnetwork)}}
VMName Network
------ -------
sql.IADPSQLHST1N01.Hosting
Can anyone please help me out!!

Try this:
Get-VM sql.IAN01.Host | Select-Object #{Name="VMName";Expression={$_.name}},#{Name='VirtualNetwork';e={$_.VirtualNetworkAdapters | Foreach-Object{$_.VirtualNetwork}}}

Related

Powershell - question about for each / variables

I have this simple question about my code. I would like to create an array for pushing my variables into database mssql.
My first problem is about the definitions of variables. The PowerShell ISE response to me is this error code:
You must provide a value expression following the '%' operator. I would like to understand how to correctly define the variables $CPUPer.CPU % and Memory (MB). I suppose it's a simple use of quotes on those properties, but i don't know what to do.
Next goal is to collect this variable and push into database table. I hope the code is correct but I probably need any suggestions.
This is an extract of my output in the array $CPUPercent :
Name CPU CPU % Memory (MB) Description
---- --- ----- ----------- -----------
dwm 1225.47 5.37 240.91796875 Desktop Window Manager
chrome 679.33 2.99 359.3828125 Google Chrome
chrome 497.44 2.19 251.390625 Google Chrome
chrome 393.58 2.15 415.515625 Google Chrome
$CPUPercent = #{Name='CPU %';Expression={$TotalSec=(New-TimeSpan -Start $_.StartTime).TotalSeconds;[Math]::Round( ($_.CPU * 100 / $TotalSec), 2)}};$CPU = #{Name='CPU';Expression={[Math]::Round($_.cpu,2)}}; Get-Process | Select -Property Name, $CPU, $CPUPercent,#{Name='Memory (MB)';Expression={($_.WorkingSet64/1MB)}}, Description | Sort -Property CPU -Descending | Format-Table -AutoSize
$CPUPercent = Get-Process | Select -Property Name, $CPU, $CPUPercent,#{Name='Memory (MB)';Expression={($_.WorkingSet64/1MB)}}, Description | Sort -Property CPU -Descending | Format-Table -AutoSize
foreach($CPUPer in $CPUPercent)
{
$name_1=$CPUPer.Name
$cpu_1=$CPUPer.CPU
$cpu_percent_1=$CPUPer.CPU %
$memory_1=$CPUPer.Memory (MB)
$descr_1=$CPUPer.Description
#$insertquery="
#INSERT INTO [dbo].[ServiceTable]
# ([Name]
# ,[CPU]
# ,[CPU_Perc]
# ,[Memory_MB]
# ,[Description])
# VALUES
# ('$name_1'
# ,'$cpu_1'
# ,'$cpu_percent_1'
# ,'$memory_1'
# ,'$descr_1')
#GO
#"
#Invoke-SQLcmd -ServerInstance 'KILIKOOD-PC\MSSQLSERVER,1433' -query $insertquery -U sa -P test123 -Database Fantasy
}
I think use ' ' for names will solve your problem, check it out
$CPUPer.'CPU %'
$CPUPer.'Memory (MB)'

Get the latest date

I have an array that I call $data.
I want to get the earliest dates from the $data array for each host in my csv file. The user will input a host and it will find the earliest date it was modified.
Hostname LastModified
HD 9/8/2012
LOG 9/15/2004
NETMAN 12/25/2004
NETMAN 5/5/2015
LOG 1/4/2013
LOG 6/6/2011
So if they input LOG, I want it to give me the earliest date.
Created on 9/15/2004
Code:
$data= import-csv ".\Earliest Date Template.csv"
$Hostname=Read-Host "Please enter Host Name"
$data | Foreach-Object {$_."Last Modified" = [DateTime]$_."Last Modified"; $_} | Group-Object Hostname| Foreach-Object {$_.Group | Sort-Object LastModified | Select-Object -First 1}
Grouping them does seem like the way to go but you don't need to do that. Just sort the entire list by date then select the last option from the list (that matches the host name you are looking for).
$hostname = Read-Host "Please enter Hostname"
$data | Sort-Object {[DateTime]$_."LastModified"} | Where-Object{$_.Hostname -eq $hostname} | Select -Last 1
You might need to do some user validation but something like this seems to work with your sample data:
Please enter Hostname: log
Hostname LastModified
-------- ------------
LOG 1/4/2013
If you then only want the date it would just be a matter of expanding the value from the result.
$data |
Sort-Object {[DateTime]$_."LastModified"} |
Where-Object{$_.Hostname -eq $hostname} |
Select -Last 1 -ExpandProperty LastModified

Powershell add like rows together value in column 2

I have been searching for a way to do this in PowerShell, but I have had no luck. I have a file that looks like this.
Column1,column2
A,1
B,2
C,3
D,4
A,2
B,3
D,6
A,3
C,2
I would like to find a way that I could sort through this file and add column2 and end up with a file that would look like this.
column1,column2
A,6
B,5
C,5
D,10
Any help would be greatly appreciated.
Combination of Group-Object and Measure-Object would accomplish this for you. Provided you have PowerShell 3.0 or higher...
Import-Csv c:\temp\test.csv | Group-Object Column1 | ForEach-Object{
[pscustomobject][ordered]#{
Column1 = $_.Name
Column2 = $_.Group.Column2 | Measure-Object -Sum | Select-Object -ExpandProperty Sum
}
} | Export-Csv -NoTypeInformation C:\temp\testOutput.csv
If you have issues with either of these cmdlets please consult the documentation or debug my script to see what it is doing.
Could also use calculated properties with the similar logic and same output. Should work on lower PowerShell versions as well.
Import-Csv c:\temp\test.csv |
Group-Object Column1 |
Select #{Label="Column1";Expression={$_.Name}},#{Label="Column2";Expression={$_.Group.Column2 | Measure-Object -Sum | Select-Object -ExpandProperty Sum}} |
Export-Csv -NoTypeInformation C:\temp\testOutput.csv

How to Parse the Results of CMDlet into Array

I am sure there is an easy answer to this question, but I cannot find it anywhere. I would like to know how to parse the results of Get-Mailbox | select Alias into an array so each time I use the array it does not show the items as "#{Alias=username}".
I have tired this, but it seems to make the values not text:
$arrayname = Get-Mailbox | select Alias
I am sure this question has been asked before, but I cannot find it.
Essentially I would like to get the Alias' from the Get-Mailbox command into an array so that I can use the foreach cmdlet to get specific folder information from a user like so:
>> $aliases = Get-Mailbox | select Alias
>> Foreach ($username in $aliases) {Get-MailboxFolderStatistics -identity $username | select FolderPath | where {$_.FolderPath -like '*Deleted*'} | Export-CSV "C:\users\username\desktop\allusers-deletedfolder-postarchive.csv" -NoTypeInformation}
The cmdlet already produces an array, only that its elements are mailbox objects, not strings. Selecting the Alias property restricts the object properties, but still leaves you with an array of objects (hence the output you observed). You need to expand the property:
$arrayname = Get-Mailbox | select -Expand Alias
or echo it in a loop:
$arrayname = Get-Mailbox | % { $_.Alias }

PowerShell script to list items in collection

I'm new to PowerShell and am trying to query against my SQL server. I get the idea of creating a new-psdrive and then navigating to databases etc and have a line of code as
$dbs = (get-childitem
sqlserver:\sql\SERVER\INSTANCE\databases)
when I pipe the $dbs to a foreach, how would I get results of a collection of the database object? I am trying to read the extendedproperties of my test database.
This single query gives the results I want repeated for each database.
set-location
DRIVENAME:\databases\beagle_test\extendedproperties
get-childitem | select displayname,
value
any help very much appreciated.
I dont have SQL server handy to try this. Let me know the result
Set-Location DRIVENAME:\Databases
Get-ChildItem | % { Get-ChildItem $("$_.Name\extendedproperties") | Select DisplayName, Value }
Try this
Set-Location DRIVENAME:\Databases
Get-ChildItem | foreach-object { if (Test-Path $("$.Name\extendedproperties")) { Get-ChildItem $("$.Name\extendedproperties") | Select DisplayName, Value } }
The second line here is a single statement. What I am doing is to check if Extendedproperties exist and then get child item.
How about:
dir sqlserver:\sql\ServerName\InstanceName\Databases\*\ExtendedProperties\* |
select #{Name="Database";Expression={$_.Parent.Name}}, Name, Value
How about just:
dir SQLSERVER:\SQL\Server\Instance\databases\*\extendedproperties\* | % {select $_.displayname, $_.value}
so, many years later I am looking into my SO stats and see this old question and figure that my powershell skills have grown a little since 2010.
The use-case has long gone but I think what I was trying to achieve is this:
foreach ($db in $SMOServer.databases | Where-Object status -eq 'normal') {
$db.ExtendedProperties | Select-Object #{name = "DBName"; expression = {$db.Name}}, name, value
}
which gives results like this:
DBName Name Value
------ ---- -----
AdventureWorks2014 MS_Description AdventureWorks 2014 Sample OLTP Database
AdventureWorks2016 MS_Description AdventureWorks 2016 Sample OLTP Database

Resources