Receiving has literal was incomplete for below query - azure

$subscriptions = Get-AzSubscription
$result = foreach ($vsub in $subscriptions){
Select-AzSubscription $vsub.SubscriptionID
Write-Host
Write-Host "Working on $($vsub.Name)"
Write-Host
foreach($VM in (Get-AzVM)){
# $Tier = (Get-AzResource -ResourceId $webapp.ServerFarmId).Sku.Tier
# $Plan = Get-AzAppServicePlan -ResourceGroupName $webapp.ResourceGroup
# output the object so it gets collected in $result
[PSCustomObject]#{
TenantId = $vsub.TenantId
SubscriptionName = $vsub.Name
VMName = $VM.Name
ResourceGroup = $VM.ResourceGroup
# Hostname = $webapp.DefaultHostName
#PricingTier = $Tier
#SKU = #($Plan.Sku.Size) -join ','
#AppServiceName = #($Plan.Name) -join ','
Status = $VM.PowerState
Location = $VM.Location
Size = $VM.HardwareProfile.VmSize
Application_Name= $VM.Tags.Application_Name
Application_Owner= $VM.Tags.Application_Owner
Business_Owner = $VM.Tags.Business_Owner
Cost_Code = $VM.Tags.Cost_Code
Created_Date = $VM.Tags.Created_Date
Environment_Name = $VM.Tags.Environment_Name
ENVIRONMENT_NAME = $VM.Tags.ENVIRONMENT_NAME
#AppType = $webapp.Kind
#SubscriptionID = $vsub.SubscriptionID
}
}
}
# sort unique and export the file
$result | Sort-Object * -Unique | Export-Csv -Path "C:\Users\Desktop\Scripts\vm_inventory.csv" -NoTypeInformation
I am trying to run this query to get the details of the VM but I am receiving the hash literal is incomplete for PSCustomObject, as per my knowledge all the brackets are proper but don't know why I am receiving the error. Request to please help me on the same.

Related

How to split different values in powershell by a line

With this script i am able to fetch all the Tags that a VM has but i want that in output the each key and its value should be separated by a line in the way that each key and its value appears on different lines like this
reference image
# Sign into Azure Portal
connect-azaccount
# Fetch the Virtual Machines from the subscription
$azureVMDetails = get-azvm
# Fetch the NIC details from the subscription
$azureNICDetails = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
#Fetching Virtual Machine Details
$virtual_machine_object = $null
$virtual_machine_object = #()
#Iterating over the NIC Interfaces under the subscription
foreach($azureNICDetail in $azureNICDetails){
#Fetching the VM Name
$azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
#Fetching the VM Tags
foreach($azureDetail in $azureVMDetails) {
$vm_tags = $azureVMDetail| Select-Object -Property (
#{name='Tags'; expression = {($_.tags.GetEnumerator().ForEach({ '{0} : {1}' -f $_.key, $_.value }) -join ';')}}
)
}
#VM Details export
$virtual_machine_object_temp = new-object PSObject
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "name" -Value $azureVMDetail.Name
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "comments" -Value ($vm_tags.Tags -join ';')
$virtual_machine_object += $virtual_machine_object_temp
}
#Report format and path
$virtual_machine_object | Export-Csv "C:\Users\JOHN\Desktop\Inventory\Final Scripts\VM_details_$(get-date -f dd.MM.yyyy).csv" -NoTypeInformation -Force
I tried to reproduce the same in my environment and got the results successfully by using the below PowerShell script:
$vmdeatil = Get-AzVm -Name testvm | Select -ExpandProperty Tags
$value = $vmdeatil
foreach($i in 0..($value.Count -1))
{
$ErrorActionPreference = ‘SilentlyContinue’
[array]$report += [pscustomobject] #{
key = $key[$i]
name = $value[$i]
}
}
$report | Export-Csv -Path "C:\Users\ruk1.csv" -NoTypeInformation
Response:
The output is successfully exported in the csv file like below:

How to add colors to Excel output file in Powershell

I have written a script to export specific registry keys and the sub keys inside it with the server ping response, but my scripts works as expected and I can able to export that to Excel as well.
But I need inputs or some help on how to add the colors to the Excel output column based on the value.
As Ex: in my script I will get ping response as true or false, for True I need to add green colour and for False I need to add Red color in my output, please help me to achieve this with my script.
CODE
## Get full list of servers
$Servers = GC -Path ".\Servers.txt"
## Loop through each server
$Result = foreach ($vm in $Servers) {
## Check the Ping reponse for each server
Write-Host "Pinging Server" $vm
$Ping = Test-Connection -Server $vm -Quiet -Verbose
if ($Ping){Write-host "Server" $vm "is Online" -BackgroundColor Green}
else{Write-host "Unable to ping Server" $vm -BackgroundColor Red}
## Check the Network Share path Accessibility
Write-Host "Checking Share Path on" $vm
$SharePath = Test-Path "\\$vm\E$" -Verbose
if ($SharePath){Write-host "Server" $vm "Share Path is Accessible" -BackgroundColor Green}
else{Write-host "Server" $vm "Share path access failed" -BackgroundColor Red}
Invoke-Command -ComputerName $vm {
## Get ChildItems under HKLM TCPIP Parameter Interface
Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces' | ForEach-Object {
Get-ItemProperty -Path $_.PSPath | Where-Object { $_.PsObject.Properties.Name -like 'Dhcp*' }
} | Select-Object -Property #{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME+"."+$env:USERDNSDOMAIN}},
#{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}},
#{Name = 'Share_Path_Access'; Expression = {if($using:SharePath) {'Accessible'} else {'Not Accessible'}}},
DhcpIPAddress, #{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
DhcpServer, #{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}}
$Result | Select-Object * -Exclude PS*, RunspaceId | Export-Excel -Path "$PSScriptRoot\TCPIP_Interface_Details.xlsx" -AutoSize -BoldTopRow -FreezeTopRow -TitleBold -WorksheetName TCPIP_Interface_Details
You can use the New-ConditionalText cmdlet to highlight cells containing the specified -Text with the color of our choice. The cmdlet can also take RGB colors. I encourage you to read the documentation on it, there are also many examples:
Get-Help New-ConditionalText
Since I don't have access to your $result object I can only give you an example of how you can do it using a simple example:
$result = 0..10 | ForEach-Object {
[pscustomobject]#{
ComputerName = 'Host' + $_
Ping_Response = ('Not Responding', 'Pinging')[($_ % 2)]
}
}
function RGB ($red, $green, $blue ){
return [System.Double]($red + $green * 256 + $blue * 256 * 256)
}
$fontGreen = RGB 0 97 0
$backGreen = RGB 198 239 206
$condProps = #{
Text = 'Pinging'
ConditionalTextColor = $fontGreen
BackgroundColor = $backGreen
}
$conditionalTrue = New-ConditionalText #condProps
$conditionalFalse = New-ConditionalText -Text 'Not Responding'
$props = #{
AutoSize = $true
InputObject = $result
Path = 'test.xlsx' # => Use your absolute Path here!
TableName = 'myTable'
TableStyle = 'Medium11'
WorksheetName = 'myWorkSheetName'
ConditionalText = $conditionalTrue, $conditionalFalse
}
Export-Excel #props
The end result should look something like this (unfortunately Google Sheets doesn't do it justice):

Powershell export CSV looks weird

I have an issue with my CSV export to Excel with powershell. When I import it looks like pretty bad and I can't find any information that helps me to solve it.
Here I attach an image of the import and the code. I see other CSV imports and it looks normal with its categories spaced by rows in Excel, but I don't know how to do it.
Image of my workbook
$Computers = Get-ADComputer -Filter {OperatingSystem -like "*Server*"} -Properties OperatingSystem | Select-Object -ExpandProperty Name
Foreach($computer in $computers){
if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer offline" -f red}
else {
$outtbl = #()
Try{
$sr=Get-WmiObject win32_bios -ComputerName $Computer -ErrorAction Stop
$Xr=Get-WmiObject –class Win32_processor -ComputerName $computer -ErrorAction Stop
$ld=get-adcomputer $computer -properties Name,Lastlogondate,operatingsystem,ipv4Address,enabled,description,DistinguishedName -ErrorAction Stop
$r="{0} GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer |Measure-Object Capacity -Sum).Sum / 1GB)
$x = gwmi win32_computersystem -ComputerName $computer |select #{Name = "Type";Expression = {if (($_.pcsystemtype -eq '2') )
{'Laptop'} Else {'Desktop Or Other something else'}}},Manufacturer,#{Name = "Model";Expression = {if (($_.model -eq "$null") ) {'Virtual'} Else {$_.model}}},username -ErrorAction Stop
$t= New-Object PSObject -Property #{
serialnumber = $sr.serialnumber
computername = $ld.name
Ipaddress=$ld.ipv4Address
Enabled=$ld.Enabled
Description=$ld.description
Ou=$ld.DistinguishedName.split(',')[1].split('=')[1]
Type = $x.type
Manufacturer=$x.Manufacturer
Model=$x.Model
Ram=$R
ProcessorName=($xr.name | Out-String).Trim()
NumberOfCores=($xr.NumberOfCores | Out-String).Trim()
NumberOfLogicalProcessors=($xr.NumberOfLogicalProcessors | Out-String).Trim()
Addresswidth=($xr.Addresswidth | Out-String).Trim()
Operatingsystem=$ld.operatingsystem
Lastlogondate=$ld.lastlogondate
LoggedinUser=$x.username
}
$outtbl += $t
}
catch [Exception]
{
"Error communicating with $computer, skipping to next"
}
$outtbl | select Computername,enabled,description,ipAddress,Ou,Type,Serialnumber,Manufacturer,Model,Ram,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,Addresswidth,Operatingsystem,loggedinuser,Lastlogondate |export-csv -Append C:\temp\VerynewAdinventory.csv -nti
}
}
As commented, your locale computer uses a different delimiter character that Export-Csv by default uses (that is the comma).
You can check what character your computer (and thus your Excel) uses like this:
[cultureinfo]::CurrentCulture.TextInfo.ListSeparator
To use Export-Csv in a way that you can simply double-click the output csv file to open in Excel, you need to either append switch -UseCulture to it, OR tell it what the delimiter should be if not a comma by appending parameter -Delimiter followed by the character you got from the above code line.
That said, your code does not produce the full table, because the export to the csv file is in the wrong place. As Palle Due commented, you could have seen that if you would indent your code properly.
Also, I would advise to use more self-describing variable names, so not $r or $x, but $memory and $machine for instance.
Nowadays, you should use Get-CimInstance rather than Get-WmiObject
AND adding to an array with += should be avoided as it is both time and memory consuming. (on every addition to an array, which is of fixed size, the entire array has to be rebuilt in memory).
Your code revised:
# set the $ErrorActionPreference to Stop, so you don't have to add -ErrorAction Stop everywhere in the script
# remember the currens value, so you can restore that afterwards.
$oldErrorPref = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# get an array of computers, gathering all properties you need
$computers = Get-ADComputer -Filter "OperatingSystem -like '*Server*'" -Properties OperatingSystem, LastLogonDate, IPv4Address, Description
$result = foreach ($computer in $computers) {
$serverName = $computer.Name
if(!(Test-Connection -ComputerName $serverName -BufferSize 16 -Count 1 -ErrorAction SilentlyContinue -Quiet)) {
Write-Host "cannot reach $serverName offline" -ForegroundColor Red
continue # skip this computer and proceed with the next one
}
try {
# instead of Get-WmiObject, nowadays you should use Get-CimInstance
$bios = Get-WmiObject -Class Win32_bios -ComputerName $serverName
$processor = Get-WmiObject -Class Win32_Processor -ComputerName $serverName
$memory = Get-WmiObject -Class Win32_PhysicalMemory -ComputerName $serverName
$disks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $serverName
$machine = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $serverName |
Select-Object #{Name = "Type"; Expression = {
if ($_.pcsystemtype -eq '2') {'Laptop'} else {'Desktop Or Other something else'}}
},
Manufacturer,
#{Name = "Model"; Expression = {
if (!$_.model) {'Virtual'} else {$_.model}}
},
UserName
# output an object to be collected in variable $result
# put the properties in the order you would like in the output
[PsCustomObject] #{
ComputerName = $serverName
Enabled = $computer.Enabled
Description = $computer.description
IpAddress = $computer.IPv4Address
Ou = $computer.DistinguishedName.split(',')[1].split('=')[1]
Type = $machine.type
SerialNumber = $bios.serialnumber
Manufacturer = $machine.Manufacturer
Model = $machine.Model
Ram = '{0} GB' -f (($memory | Measure-Object Capacity -Sum).Sum / 1GB)
ProcessorName = $processor.Name
NumberOfCores = $processor.NumberOfCores
NumberOfLogicalProcessors = $processor.NumberOfLogicalProcessors
Addresswidth = $processor.Addresswidth
OperatingSystem = $computer.OperatingSystem
# {0:N2} returns the number formatted with two decimals
TotalFreeDiskSpace = '{0:N2} GB' -f (($disks | Measure-Object FreeSpace -Sum).Sum / 1GB)
LoggedInUser = $machine.UserName
Lastlogondate = $computer.LastLogonDate
}
}
catch {
Write-Warning "Error communicating with computer $serverName, skipping to next"
}
}
# restore the ErrorActionPreference to its former value
$ErrorActionPreference = $oldErrorPref
# output the completed array in a CSV file
# (using the delimiter characer your local machine has set as ListSeparator)
$result | Export-Csv -Path 'C:\temp\VerynewAdinventory.csv' -UseCulture -NoTypeInformation

Run Powershell Cmdlet with various number of parameters

I'm scripting some configuration of Azure Analysis Service, and more specifically the firewall. The cmdlet
New-AzAnalysisServicesFirewallConfig -FirewallRule
takes in the rules created as a parameter; $rule1 as an example.
I want the script to be able to take a varying number of parameters to that command. Instead of having to hardcode it to X number of rules like in the example with 6 rules.
I tried to create an Array and a Hashtable containing the X number of parameters, but it's unable to convert them.
$FirewallRules | ForEach-Object {
$ruleNumberVar = "rule" + "$ruleNumberIndex"
if (!($_.FirewallRuleName -match "$ExistingFirewallRuleName")) {
$start = $_.RangeStart
$end = $_.RangeEnd
$tempRule = New-AzAnalysisServicesFirewallRule `
-FirewallRuleName $_.FirewallRuleName `
-RangeStart $start `
-RangeEnd $end
Set-Variable -Name "$ruleNumberVar" -Value $tempRule
$ruleNumberIndex = $ruleNumberIndex + 1
}
$conf = New-AzAnalysisServicesFirewallConfig -FirewallRule $rule1,$rule2,$rule3,$rule4,$rule5,$rule6
Getting this error:
New-AzAnalysisServicesFirewallConfig : Cannot bind parameter 'FirewallRule'. Cannot convert the "System.Collections.Hashtable" value of type "System.Collectio
ns.Hashtable" to type "Microsoft.Azure.Commands.AnalysisServices.Models.PsAzure
AnalysisServicesFirewallRule".
This is untested and therefore theoretical, but this should fix that error if the type can be casted.
$Rules = #() -as [System.Collections.Generic.List[Microsoft.Azure.Commands.AnalysisServices.Models.PsAzureAnalysisServicesFirewallRule]]
$FirewallRules | ForEach-Object {
$ruleNumberVar = "rule" + "$ruleNumberIndex"
if (!($_.FirewallRuleName -match "$ExistingFirewallRuleName")) {
$start = $_.RangeStart
$end = $_.RangeEnd
$tempRule = New-AzAnalysisServicesFirewallRule `
-FirewallRuleName $_.FirewallRuleName `
-RangeStart $start `
-RangeEnd $end
Set-Variable -Name $ruleNumberVar -Value $tempRule
$Rules.Add((Get-Variable $ruleNumberVar -ValueOnly))
$ruleNumberIndex = $ruleNumberIndex + 1
}
}
$conf = New-AzAnalysisServicesFirewallConfig -FirewallRule $Rules

Powershell: Get AzureRM automation schedule recurrence info

I'm doing some inventory trying to gather all my start/stop VM schedules from Azure.
I'm strugling with extracting the days selected for weekly recurrence schedules.
I can extract all the data from single schedules with:
Select-AzureRmSubscription <name>
$schedule = Get-AzureRmAutomationSchedule -AutomationAccountName <name)-ResourceGroupName <name> -Name <name>
And then get all the days:
$schedule.WeeklyScheduleOptions.DaysOfWeek -join ","
Which outputs: Monday,Tuesday,Wednesday,Thursday,Friday
But if I loop through all my subscriptions and build a psobject
with all schedule data this data comes up empty:
$AzSubs = Get-AzureRmSubscription
$objs = #()
foreach ($AzSub in $AzSubs){
Get-AzureRmSubscription -SubscriptionName $AzSub.Name | Select-AzureRmSubscription
$azAutAccs = Get-AzureRmAutomationAccount
foreach ($azAutAcc in $azAutAccs){
$AzAutScheds = Get-AzureRmAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName
$AzAutScheds = $AzAutScheds | where{$_.IsEnabled -eq "True"}
foreach ($AzAutSched in $AzAutScheds){
$DOW = $azAutSched.WeeklyScheduleOptions.DaysOfWeek -join "," | out-string
$DOM = $azAutSched.MonthlyScheduleOptions.DaysOfMonth -join "," | out-string
$obj = new-object psobject -Property #{
SchedName = $AzAutSched.Name
LastModifiedTime = (get-date ([DateTime]::Parse($AzAutSched.LastModifiedTime)) -Format "dd-MM-yyyy HH:mm (zzz)")
IsEnabled = $AzAutSched.IsEnabled
AutomationAccount = $azAutAcc.AutomationAccountName
ResourceGroup = $azAutAcc.ResourceGroupName
NextRun = ([DateTime]::Parse($azAutSched.NextRun))
StartTime = (get-date ([DateTime]::Parse($azAutSched.StartTime)) -Format "HH:mm (zzz)")
TimeZone = $azAutSched.TimeZone
Interval = $azAutSched.Interval
Frequency = $azAutSched.Frequency
WeekSchedule = $DOW
MonthSchedule = $DOM
}
$objs += $obj
}
}
}
$objs | sort SchedName | ft -Property SchedName,LastModifiedTime,StartTime,TimeZone,Interval,Frequency,WeekSchedule,MonthSchedule
Then my table ends up with just blank columns for WeekSchedule/MonthSchedule.
I have tried different combos of leaving out the out-string parameter, leaving out the join, setting the property directly in the property line, and as quoted building the variable above the object and referencing it on the property line. None of them work.
Anyone can shed some light as to what I am missing? Or other hints on how to accomplish this are most welcome.
AzureRM module is up to date.
According to my test you need to get individual schedule, not all the schedules in the resource group, it will work in this case:
foreach ($azAutAcc in $azAutAccs){
$AzAutScheds = Get-AzAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName
$AzAutScheds = $AzAutScheds | Where-Object {$_.IsEnabled -eq "True"}
foreach ($AzAutSched in $AzAutScheds){
$AzAutSched = Get-AzAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName -Name $AzAutSched.Name
$DOW = $azAutSched.WeeklyScheduleOptions.DaysOfWeek -join "," | out-string
$DOM = $azAutSched.MonthlyScheduleOptions.DaysOfMonth -join "," | out-string
$objs += new-object psobject -Property #{
SchedName = $AzAutSched.Name
LastModifiedTime = (get-date ([DateTime]::Parse($AzAutSched.LastModifiedTime)) -Format "dd-MM-yyyy HH:mm (zzz)")
IsEnabled = $AzAutSched.IsEnabled
AutomationAccount = $azAutAcc.AutomationAccountName
ResourceGroup = $azAutAcc.ResourceGroupName
NextRun = ([DateTime]::Parse($azAutSched.NextRun))
StartTime = (get-date ([DateTime]::Parse($azAutSched.StartTime)) -Format "HH:mm (zzz)")
TimeZone = $azAutSched.TimeZone
Interval = $azAutSched.Interval
Frequency = $azAutSched.Frequency
WeekSchedule = $DOW
MonthSchedule = $DOM
}
}
}

Resources