Fetching Log Analytics Primary Key - azure

I was trying to get the Azure Log Analytics Workspace primary key by using this command:
Get-AzOperationalInsightsWorkspaceSharedKey -ResourceGroupName "abc-rg" -Name "abc-LAW"
The above command outputs both primary & secondary keys. But how can I just get the primary key to a variable and use it in my PowerShell code?
I was trying this line to get the FIRST key:
(Get-AzOperationalInsightsWorkspaceSharedKey -ResourceGroupName aby-rg -Name abys-LAW).value[0]
but I get an error:
Cannot index into a null array.
At line:36 char:1
[string]$omsSharedKey = (Get-AzOperationalInsightsWorkspaceSharedKey ...
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Thanks,

The output of the Get-AzOperationalInsightsWorkspaceSharedKey cmdlet is an object of type PSWorkspaceKeys, with two properties:
PrimarySharedKey
SecondarySharedKey
So you should be able to get to the PrimarySharedKey with $keys.PrimarySharedKey.
When in doubt, you can use the GetType method on the object to know its type and Get-Member cmdlet to view the complete listing of its members.
PS C:\> $keys = Get-AzOperationalInsightsWorkspaceSharedKey -ResourceGroupName <ResourceGroup> -Name <WorkspaceName>
PS C:\> $keys
PrimarySharedKey : 5LLi6guNfYKJ0ipSY9cv0NJ8ZeOAHlz7dX0hI9S5RTI6ISnGwa3aTvq/Mzg2SlwaaRqkZAAaSiYStlpx48y8zQ==
SecondarySharedKey : YAQo85BdaEc+W7GD2BCmXbvMFtS9XkMYui6pECl6dZS9rqk7gZjy5wo7s0/Tk+Ceq9zaNW9bbggSUaYzpeUrXw==
PS C:\> $keys.PrimarySharedKey
5LLi6guNfYKJ0ipSY9cv0NJ8ZeOAHlz7dX0hI9S5RTI6ISnGwa3aTvq/Mzg2SlwaaRqkZAAaSiYStlpx48y8zQ==
PS C:\> $keys.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSWorkspaceKeys System.Object
PS C:\> $keys | Get-Member -MemberType Properties
TypeName: Microsoft.Azure.Commands.OperationalInsights.Models.PSWorkspaceKeys
Name MemberType Definition
---- ---------- ----------
PrimarySharedKey Property string PrimarySharedKey {get;set;}
SecondarySharedKey Property string SecondarySharedKey {get;set;}
Reference: Get-AzOperationalInsightsWorkspaceSharedKey

Fetching Log Analytics workspace primary key using azure cli
az monitor log-analytics workspace get-shared-keys --resource-group $ResourceGroup --workspace-name $loganalyticsname --query "primarySharedKey"
Reference Azure Documentation

Related

Copy account creation date time value to an extension attribute in Active Directory

I'm trying to create a PowerShell script that copies an AD user account's creation date and time value to the extension attribute 2 field in the user's account. Here's the contents of my script:
$users = get-aduser -Filter * -Properties * -SearchBase "DC=TestDC1" | Where-Object {$_.whenCreated -ne $null }| Select-Object Samaccountname,whenCreated
foreach($user in $users)
{
set-aduser -identity $user.Samaccountname -add #{extensionAttribute2=$user.whenCreated}
}
When I run the script, it fails with the following error:
set-aduser : Invalid type 'System.DateTime'.
Parameter name: extensionAttribute2
At C:\Users\admin\Documents\PowerShell\Scripts\Update_User_AccountCreation_Attribute.ps1:7 char:1
+ set-aduser -identity $user.Samaccountname -add #{extensionAttribute2= ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (john.sp.smith:ADUser) [Set-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
ands.SetADUser
I believe this is because extension attributes in AD only accept text-based values by default. How can I update the script to convert the account creation date and time value to a string that I can copy to extension attribute 2?
You just need to convert the DateTime to a string. You can do that with .ToString().
set-aduser -identity $user.Samaccountname -add #{extensionAttribute2=$user.whenCreated.ToString()}
If you want to use a specific date format, you can read the documentation for ToString().

Powershell workflow with "foreach -parallel" and Invoke-AZVMRunCommand- how to get results out?

I have a bunch of VMs in Azure I want to run the same script against. Because of reasons, I can't use powershell 7, so I have to use powershell 5. Which means I can use "foreach -parallel", but NOT "foreach-parallel". So it can work, but requires a workflow.
I have the following workflow that will grab the list of servers in a specific tenant/environment, and then run a foreach -parallel to run a script against each of them at the same time.
The problem I'm running into is that it appears to work, but the result I get out is:
Microsoft.Azure.Commands.Compute.Automation.Models.PSRunCommandResult
blah
Which is not the actual result (in this it would be the time). Because it's within a workflow, getting the details out is... painful. How do I get the results? It appears to run, there's no errors, but I can't figure out the results. Thanks.
The get-member on $out gives:
Name MemberType Definition
---- ---------- ----------
GetType Method type GetType()
ToString Method string ToString(), string ToString(string format, System.IFormatProvider formatProvider), string IFormattable.ToString(string format, System.I...
PSComputerName NoteProperty string PSComputerName=localhost
PSShowComputerName NoteProperty bool PSShowComputerName=True
PSSourceJobInstanceId NoteProperty guid PSSourceJobInstanceId=0870d1ff-1234-5678-014e-2e123456c7d8
Capacity Property System.Int32 {get;set;}
Count Property System.Int32 {get;set;}
EndTime Property {get;set;}
Error Property {get;set;}
Item Property {get;set;}
Name Property {get;set;}
Output Property {get;set;}
StartTime Property {get;set;}
Status Property System.String {get;set;}
Value Property Deserialized.System.Collections.Generic.List`1[[Microsoft.Azure.Management.Compute.Models.InstanceViewStatus, Microsoft.Azure.Management.Compu...
Microsoft.Azure.Commands.Compute.Automation.Models.PSRunCommandResult
And here's the script I'm running:
#for some reason you need to disconnect from one and then connect to the other, can't run two at once.
disconnect-azaccount
connect-azaccount -Tenant "mytenantid"
#get all the dev Windows VM servers in the current tenant that are turned on.
$serverinfo = #()
Get-AzContext -ListAvailable |where-object {$_.Name -like "*Dev*"}| %{
$_|select-azcontext
$serverinfo += get-azvm -status | Where-Object {$_.PowerState -eq "VM running" -and $_.StorageProfile.OSDisk.OSType -eq "Windows"}
}
#honestly, for the test I filtered down to the jumpboxes.
$serverinfo2 = $serverinfo|where {$_.Name -like "*jumpbox*"}
#workflows are needed; can't run foreach -parallel otherwise.
Workflow TestParallel{
#need to pass in the details, it can't reach the $serverinfo2 otherwise.
param($listofservers) #take serverinfo2 and make it accessible from within the workflow.
$test=#()
#need to set the proper context
Get-AzContext -ListAvailable |where-object {$_.Name -like "*Dev*"}| select-azcontext
#now we run each of these at the same time. Foreach-parallel requires 7, but we can do foreach -parallel within powershell 4 workflows.
Foreach -parallel($server in $listofservers){
#now that the script is on there, run the command locally.
$out = Invoke-AzVMRunCommand `
-ResourceGroupName $server.ResourceGroupName `
-Name $server.name `
-CommandId 'RunPowerShellScript' `
-ScriptString "$dt = gwmi win32_localtime; New-Object DateTime $dt.year,$dt.month,$dt.day,$dt.hour,$dt.minute, $dt.second"
#scriptstring is a newer command, added in july 2022
#Formating the Output with the VM name. Value[0].Message contains the results from running the script.
#export it to a variable that will survive the foreach
#$WORKFLOW:test += $server.Name + " " + $out.Value[0].Message
$WORKFLOW:test += $out.Value[1].Message #appears to be 1 now, using " -scriptstring"
#writing it locally to see if it shows up
"$out"
}
[array]$resultsArray = #($test)
write-output ("blah " + $test[0])
$resultsArray
}
TestParallel $serverinfo2

overwriting tag Name on azure resourcegroup with powershell

Below is the azure resource
PS C:\Windows\System32> Get-AzResource -ResourceGroupName paniRG
Name : paniavset123
ResourceGroupName : paniRG
ResourceType : Microsoft.Compute/availabilitySets
Location : eastus
ResourceId : /subscriptions/8987b447-d083-481e-9c0f-f2b73a15b18b/resourceGroups/paniRG/providers/Microsoft.C
ompute/availabilitySets/paniavset123
Tags :
Name Value
============== ======
CostCenter ABCDEG
Owner john
classification Public
From the above output I want to replace Tag Name "classification" with "Classification" and "Public" with "Accounts".
I am following the below procedure to do that.
PS C:\Windows\System32> $tags=#{"Classification"="Accounts"}
PS C:\Windows\System32> $s=Get-AzResource -ResourceGroupName paniRG
PS C:\Windows\System32> Update-AzTag -ResourceId $s.ResourceId -Tag $tags -Operation Merge
Id : /subscriptions/8987b447-d083-481e-9c0f-f2b73a15b18b/resourceGroups/paniRG/providers/Microsoft.Compute/availabilitySets/
paniavset123/providers/Microsoft.Resources/tags/default
Name : default
Type : Microsoft.Resources/tags
Properties :
Name Value
============== ========
CostCenter ABCDEG
Owner john
classification Accounts
When I use update-aztag command Tag Value is changing but Name its still showing as "classification".
Can anyone please help me on this?
You could remove the classification tag from the hash table resource Tags, then add a new Classification tag with the new Account value. You can then modify the newly updated hashtable with Set-AzResource.
$resource = Get-AzResource -Name "RESOURCE NAME" -ResourceGroupName "RESOURCE GROUP NAME"
# Get the tags
$tags = $resource.Tags
# Check if hashtable has classification key
if ($tags.ContainsKey("classification")) {
# Remove classification key if it exists
$tags.Remove("classification")
}
# Create Classification tag with Accounts value
$tags.Classification = "Accounts"
# Update resource with new tags
$resource | Set-AzResource -Tag $tags -Force
If you want to use Update-AzTag, you will need to use a Replace operation:
Update-AzTag -ResourceId $s.Id -Tag $tags -Operation Replace
The Merge operation will update the value, but not the key name.
Just use -Operation Replace not -Operation Merge
Update-AzTag -ResourceId $s.ResourceId -Tag $mergedTags -Operation Replace

How can I simply get the powerstate from within a powershell workflow automation runbook in Azure?

I have a Powershell workflow runbook that automates starting and shutting down VMs in Azure, I updated the modules in an automation account (so I could use it for other things) and it has stopped the script working. I have fixed most of the broken stuff but the bit that is not now working is obtaining the power state eg: PowerState/deallocated so that it can be shutdown/started up. Here is my code:
$vmFullStatus = Get-AzureRmVM -ResourceGroupName test1 -Name test1 -Status
$vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
$vmStatus = $vmStatusJson | ConvertFrom-Json
$vmStatusCode = $vmStatus.Statuses[1].code
Write-Output " VM Status Code: $vmStatusCode"
The Write-Output VM Status Code is now blank in the output of the runbook, but it outputs fine in standard shell. I only have limited experiences in workflow runbooks but I believe it needs to be converted to Json so the Workflow can use it.
I think the issue may lie with the statuses as when it is converted to Json it displays:
"Statuses": [
"Microsoft.Azure.Management.Compute.Models.InstanceViewStatus",
"Microsoft.Azure.Management.Compute.Models.InstanceViewStatus"
],
Which doesn't now show the PowerState. How can I get the powerstate of a vm from within a powershell workflow runbook so it can used? Thanks
I have tried an inline script and it does work if you specify a vm name:
$vmStatusCode = InlineScript {
$vmFullStatus = Get-AzureRmVM -ResourceGroupName test1 -Name test1 -Status
$vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
$vmStatus = $vmStatusJson | ConvertFrom-Json
$vmStatus.Statuses[1].code
}
But it doesn't work when you pass variables:
$vmFullStatus = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
Get-AzureRmVM : Cannot validate argument on parameter 'ResourceGroupName'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
it needs to be run without an inline script - any ideas?
forgot to add $using:
$vmStatusCode = InlineScript {
$vmFullStatus = Get-AzureRmVM -ResourceGroupName $using:vm.ResourceGroupName -Name $using:vm.Name -Status
$vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
$vmStatus = $vmStatusJson | ConvertFrom-Json
$vmStatus.Statuses[1].code
}
This now works!

How do i get a list of Vm in a recourse group

Im trying to get all Vms in a resource group then send every element to a function
$a = Get-AzureRmVM -ResourceGroupName Test2 | ft Name
foreach($output in $a) {Stop-AzRmVM -ResourceGroupName "Test2" -Name $output}
Im getting this error message
Start-AzureRmVM : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified
method is not supported.
Anish K is correct about the Name parameter expecting a String, not an array of objects.
However, you should also remove the | ft Name because that is only for outputting stuff to console.
I'd use a ForEach-Object here like this (untested):
Get-AzureRmVM -ResourceGroupName 'Test2' | ForEach-Object {
$_ | Stop-AzRmVM
}
As per the error, you are passing an object[] instead of string. Your function Stop-AzRmVm expects string for Name parameter.
Changes your script like below:
$a = Get-AzureRmVM -ResourceGroupName Test2 | ft Name
foreach($output in $a) {Stop-AzRmVM -ResourceGroupName "Test2" -Name $output.Name}

Resources