Is there a way to find a Removed Keyvault by it's tags - azure

I need to find the KeyVaultname of my removed keyvault (by softdelete) by its specifics tags.
This is the keyvault I need to find:
KeyVault In Removed State
Unfortunately the command to find the tags for a keyvault by below cmd doesn't work for a Keyvault which is in a Removed state. (It works when the KeyVault is not removed)
(Get-AzKeyvault -InRemovedState -tag #{"RemovalDate" = "14-04-2022"})
Gives the following error:
Get-AzKeyVault : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:2
+ (Get-AzKeyvault -InRemovedState -tag #{"RemovalDate" = "14-04-2022"})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzKeyVault], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Azure.Commands.KeyVault.GetAzureKeyVault
I tried as well:
(Get-AzKeyvault -InRemovedState | Where-Object {$_.Tag["RemovalDate"] -eq "14-04-2022"})
Which gives the following error:
Cannot index into a null array.
At line:1 char:49
+ ... RemovedState | Where-Object {$_.Tag['RemovalDate'] -eq '14-04-2022'})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Thank you very much for your help!

You were close, just needed to pipe results of the Get-AzKeyVault, then query it like any other object.
First, find out the object types that are returned.
Get-AzKeyVault -InRemovedState | get-member
This displays the column names and object type. Now write your query appropriately.
Get-AzKeyVault -InRemovedState |
select VaultName, Tags |
where {$_.Tags["RemovalDate"] -eq "14-04-2022"}

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 script works in ISE / Console but not Task Scheduler

I currently have a script that allows me to convert any excel spreadsheets within a specific folder to PDF documents which are saved in another folder. The script works great if you run it from Powershell ISE / Console. However, if you run the script by creating a task within Windows Task Scheduler it fails.
ZAOCC.ps1
Start-Transcript -Path 'C:\Temp\Log.txt'
Install-Module -Name ImportExcel -Force
$path = 'C:\Temp\Excel'
$path2 = 'C:\Temp\PDF'
$xlFixedFormat = 'Microsoft.Office.Interop.Excel.xlFixedFormatType' -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
$date = Get-Date -Format 'dd.MM.yyyy'
foreach($wb in $excelFiles)
{
$filepath = Join-Path -Path $path2 -ChildPath ('Mine Control Record - ' + $date + '.pdf')
$workbook = $objExcel.workbooks.open($wb.fullname, 3)
$workbook.Saved = $true
"Saving $filepath"
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
$objExcel.Quit()
I have included Start-Transcript and found the following errors when running the script through task scheduler:
Microsoft Excel cannot access the file 'C:\Temp\Excel\Test.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
At C:\Temp\ZAOCC.ps1:16 char:5
+ $workbook = $objExcel.workbooks.open($wb.fullname, 3)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Microsoft Excel cannot access the file 'C:\Temp\Excel\Test.xlsx'. There are several possible
reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
At C:\Temp\ZAOCC.ps1:16 char:5
+ $workbook = $objExcel.workbooks.open($wb.fullname, 3)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The property 'Saved' cannot be found on this object. Verify that the property exists and can be set.
At C:\Temp\ZAOCC.ps1:17 char:5
+ $workbook.Saved = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The property 'Saved' cannot be found on this object. Verify that the property exists and can be
set.
At C:\Temp\ZAOCC.ps1:17 char:5
+ $workbook.Saved = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Saving C:\Temp\PDF\Mine Control Record - 10.07.2020.pdf
You cannot call a method on a null-valued expression.
At C:\Temp\ZAOCC.ps1:19 char:5
+ $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Temp\ZAOCC.ps1:19 char:5
+ $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS>$global:?
True
My task within Task Scheduler is set to run with admin privileges with the following settings:
Program/Script powershell.exe
Add arguments(optional) -ExecutionPolicy Bypass C:\Temp\ZAOCC.ps1
Any help would be appreciated!
Solution turned out to be an excel bug. Followed the solution here: Powershell script cannot access a file when run as a Scheduled Task

Azure Resource Groups by Tag

Trying to list all resource groups that contain a given tag value.
I was able to get a list of resources when hardcoding the value of the tag, but I am not successful when passing a variable containing the value.
getting the following error:
+ $resourceGroups = (Get-AzureRmResourceGroup -Tag #{ $Tag}).ResourceGr ...
+ ~
Missing '=' operator after key in hash literal.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral
not sure how to make the command evaluate the value of $Tag
i think you should do something like this:
$tag = #{ "name" = "value" }
Get-AzureRmResourceGroup -Tag $Tag
Just to elaborate, your parameter for tag is #{$tag} which means it interprets that you're specifying a hashtable instead of passing a hashtable. Your $tag variable will/should already be a hashtable so your command will look like what #4c74356b41 has specified
$resourceGroups = (Get-AzureRmResourceGroup -Tag $Tag).ResourceGroupName

I can't delete azure alert

i have the following alert in Azure:
PS C:\> Get-AzResource -Name "alert for rg"
Name : alert for rg ResourceGroupName : plaz-rg2 ResourceType : Microsoft.AlertsManagement/actionRules Location : global ResourceId : /subscriptions/XXXX/resourceGroups/plaz-rg2/providers/Microsoft.Alerts
Management/actionRules/alert for rg
I was deleting it before but it is still visible.
I can't delete resource group because of it.
PS C:\> Remove-AzResourceGroup -name "plaz-rg2"
Confirm
Are you sure you want to remove resource group 'plaz-rg2'
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
Remove-AzResourceGroup : Long running operation failed with status 'Conflict'.
At line:1 char:1
+ Remove-AzResourceGroup -name "plaz-rg2"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzResourceGroup], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.RemoveAzureResourceGroupCmdlet
When I'm trying to delete the alert alone it is not possible
PS C:\> Remove-AzResource -Name "alert for rg" -ResourceType Microsoft.AlertsManagement/actionRules
Confirm
Are you sure you want to delete the following resource:
/subscriptions/XXXX/providers/Microsoft.AlertsManagement/actionRules/alert%20for%20rg
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
Remove-AzResource : ResourceNotFound : The Resource 'Microsoft.AlertsManagement/actionRules/alert for rg' under resource gr
oup '<null>' was not found.
At line:1 char:1
+ Remove-AzResource -Name "alert for rg" -ResourceType Microsoft.Alerts ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzResource], ErrorResponseMessageException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.RemoveAzureResourceCmdlet
Any idea how to get rid of it?
I see this behavior if we are not passing the ResourceGroupName as input parameter along with Remove-AzResource.
However if you pass in the ResourceGroupName it runs successfully. Please try this out.
Additional documentation reference to remove AlertRule.
Hope this helps.

Can't call up SpFeature by Display Name - sharepoint 2010 power shell

I am running the sharepoint 2010 Management Shell and I am did this
Get-SPFeature –DocumentRoutingResources –Site http://sp2010 |ft -auto
Get-SPFeature : A parameter cannot be
found that matches parameter name
'Docume ntRoutingResources'. At line:1
char:40
+ Get-SPFeature -DocumentRoutingResources <<<< -Site http://sp2010 |ft -auto
+ CategoryInfo : InvalidArgument: (:) [Get-SPFeature],
ParameterB indingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.SharePoint.Powe
rShell.SPCmdletGetFeature
I am not sure why I get that since when I just do
Get-SPFeature –Site http://sp2010
It shows up
The code you entered is passing a parameter called DocumentRoutingResources to the PowerShell command, which doesn't have such a parameter.
If you want just that item returned, you can filter for it quite easily:
Get-SPFeature -site http://tskm | ? {$_.DisplayName -eq "DocumentRoutingResources" }
The "?" is a shortcut for the cmdlet "where-object".
For your specific example, the cmdlet supports the 'identity' parameter as shown here:
Get-SPFeature -identity DocumentRoutingResources -Site http://sp2010

Resources