Azure PowerShell :: how to print a list of properties - azure

I found on this post how to print the the Azure Subscription ID:
(Get-AzContext).Subscription.id
But if I look in the official documentation of the command Get-AzContext I don't see anywhere that the .Subscription.id or .id would print that information.
How the guy who replied to that question knew such information?
Where can I find a list of properties for each command?
Commmands like Get-AzContext | fl * or Get-AzContext | gm or get-help Get-AzContext -full don't provide such list.
I want to be able to see all properties provided by commands like Get-AzResource or Get-AzSqlDatabase or any other.

Problably not the cleanest way, but as I use this trick very often and since I shared to some teammates I noticed they are using it now I guess it worths sharing :) .
Use the convertto-json -depth xx (where xx is big enough for your need and depending on the objet's complexity) to get the whole view of an object
Then you can redirect to a file and look for what you need quite easily.
In case you run Get-AzContext | convertto-json -depth 10 you will find back the subscription and the ID.

Related

How to increase output rows in Resource Graph Query in Azure

I am using Resource Graph Powershell module to query all the recent change details in my Subscription.
https://learn.microsoft.com/en-us/azure/governance/resource-graph/how-to/get-resource-changes?tabs=azure-powershell
But though there are many changes happened, the query is returning at max 100 at a time. So is there a way to get the complete records?
Is there a way to get all available records from Resource Graph module?
I have reproduced in my environment and got expected results as below and I followed Microsoft-Document:
$count= Search-AzGraph -Query 'resourcechanges | project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes' -First 1000
$count | Measure-Object
So you need to use -First Paramter
Or you can use the query directly in Azure Resource graph Explorer as below:

PowerShell: update O365 AD bulk attributes through csv file

We are trying to bulk update our Azure Active Directory. We have a excel csv list of UserPrincipalNames that we will update the Title, Department, and Office attributes
# Get List of Clinical CMs
$PATH = "C:\Users\cs\Documents\IT Stuff\Project\Azure AD Update\AD-Update-ClinicalCMs-Test.csv"
$CMs = Import-csv $PATH
# Pass CMs into Function
ForEach ($UPN in $CMs) {
# Do AD Update Task Here
Set-Msoluser -UserPrincipalName $UPN -Title "Case Manager" -Department "Clinical" -Office "Virtual"
}
The CSV:
User.1#domain.com
User.2#domain.com
User.3#domain.com
The Set-MsolUser command will work on its own, but it is not working as intended in this For loop. Any help or insight is greatly appreciated
As Jim Xu commented, here my comment as answer.
The input file you show us is not a CSV file, instead, it is a list of UPN values all on a separate line.
To read these values as string array, the easiest thing to is to use Get-Content:
$PATH = "C:\Users\cs\Documents\IT Stuff\Project\Azure AD Update\AD-Update-ClinicalCMs-Test.csv"
$CMs = Get-Content -Path $PATH
Of course, although massive overkill, it can be done using the Import-Csv cmdlet:
$CMs = (Import-Csv -Path $PATH -Header upn).upn

Call Set-AzureRmAppServicePlan cmdlet with arbitrary parameters

I want to call Set-AzureRmAppServicePlan with arbitrary parameters, the list of parameters is defined in runtime and is not set statically.
In other languages like Perl I would use Hash for this, but I get stuck here in Powershell even though I know that Powershell supports HashTables.
Following example does not work
$params = #{}
$params.add('-WorkerSize', 'Small');
$params.add('-NumberofWorkers', '2');
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' $params
I get Set-AzureRmAppServicePlan : Long running operation failed with status 'BadRequest'. error from Azure.
PowerShell does support what you are trying to do. It is known as splatting.
In your case, you have a minor typo. All you need to do is change the $ to an # in this line:
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' $params
So the working version looks like this:
Set-AzureRmAppServicePlan -ResourceGroupName 'RG1' -Name 'AppServicePlna1' #params
The issue is not with PowerShell per say.
Last time I checked you cannot pass 'Parameter key' for an Azure Module command.

String Length different when using ISE or Powershell.exe

I have written a small user Interface with PowerShell in order to create checksums comfortable. At the moment I see a difference between using the ISE and the powershell itself.
When running the PowerShell script within the ISE I get perfect results.
This is the code I am using (Just a snippet):
$aaa = (Get-FileHash -Algorithm SHA512 -Path $str_filepath |
Select-Object -Property hash |
Format-Table -HideTableHeaders |
Out-string).TrimEnd().TrimStart()
So I create a checksum (SHA512) from a file. These are the results for one file as example:
Running the script in ISE:
0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8884E4715C8A0
Running the script in PowerShell:
0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8...
As you can see the string is shortend when using PowerShell. More confusing is that the shortening is not consistent. At home on my Windows 7 machine the string is even shorter then on my Windows 8.1 System at work. I know that there are some differences between ISE and PowerShell when running scripts regarding to styles. But shorter strings... Hmm.
So now the question. Does anyone of you have expierenced that difference between ISE and Powershell regarding to String length limitations? And if so. Does have anyone an answer for me how I can script it that there will be no different string results?
Do not use Format-* cmdlets if you need to process your data further. Those cmdlets are only for displaying data to a user. Instead expand the Hash property of the object Get-FileHash returns, either like this:
$str_checksum_sha512 = (Get-FileHash -Algorithm SHA512 -Path $str_filepath).Hash
or like this:
$str_checksum_sha512 = Get-FileHash -Algorithm SHA512 -Path $str_filepath |
Select-Object -Expand Hash

Trying to Export a CSV list of users using Active Directory Module for Windows Powershell

So the below is where I'm at so far:
import-module activedirectory
$domain = "ourdomain"
Get-ADUser -Filter {enabled -eq $true} -Properties whenCreated,EmailAddress,CanonicalName |
select-object Name,EmailAddress,CanonicalName,whenCreated | export-csv C:\Data\test.csv
Unfortunately, when I run the above I get dates in two different formats in the CSV, e.g.:
01/01/2017
1/01/2017 8:35:56 PM
The issue this poses is that there isn't really a clean way to sort them. Excel's formatting doesn't change either of these formats to be more like the other, both because of the inclusion of time in one and not the other, and because the time-inclusive format doesn't use trailing zeroes in the single digit numbers, but the time-exclusive format does.
We have an existing script that captures users using the LastLogonTimestamp attribute that does this correctly by changing the bottom line to the following:
select-object Name,EmailAddress,CanonicalName,#{Name="Timestamp"; Expression={[DateTime]::FromFileTime($_.whenCreated).ToString('yyyy-MM-dd_hh:mm:ss')}}
For some reason this expression runs properly when we query the LastLogonTimestamp attribute, but when we run this version querying the whenCreated attribute, we get an entirely blank column underneath the Timestamp header.
I'm not particularly knowledgeable about PowerShell itself, and my colleague who had found the original script for the LastLogonTimestamp just found it online and adapted it as minimally as possible to have it work for us, so I don't know if something in this line would work properly with one of these attributes and not the other. It seems strange to me though that two attributes using dates in the same program would store them in different formats though, so I'm not convinced that's it.
In any case, any help anyone can offer to help us get a uniform date format in the output of this script would be greatly appreciated - it needn't have the time included if it's easier to do away with it, though if they're equally easy we may as well keep it.
whencreated is already a [DateTime]. Notice the difference between the properties when you run something like this:
Get-ADUser TestUser -Properties lastlogon,whenCreated | select lastlogon,whenCreated | fl
(Get-ADUser TestUser -Properties lastlogon).lastlogon | gm
(Get-ADUser TestUser -Properties whenCreated).whenCreated | gm
This means that you don't have to convert to a DateTime before running the toString() method.
select-object #{Name="Timestamp"; Expression={$_.whenCreated.ToString('yyyy-MM-dd_hh:mm:ss')}}

Resources