I followed through.
Set-AzureADUserManager -ObjectId "df19e8e6-2ad7-453e-87f5-037f6529ae16" -RefObjectId "df19e8e6-2ad7-453e-87f5-037f6529ae16".
No Luck. Through script also unable to do.
Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is null.
At line:7 char:61
+ ... ger -ObjectId (Get-AzureADUser -ObjectId $row.'User Username').Object ...
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-AzureADUser], ParameterBindingValidationExcep
tion
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.Azure
AD16.PowerShell.GetUser.
The error message means that $row.'User Username' does not have a value.
As a test to confirm this you could add a line to your code to write the value to the console such as Write-Host $row.'User Username'
The value expected by the Get-AzureADUser cmdlet is in fact the ObjectId so the line does not make much sense to me anyways. Why would you run a cmdlet to get the ObjectId when you already have it in $row.'User Username'.
You should either confirm that $row.'User Username' is not $null and contains the ObjectId or use one of the other alternatives outlined in the examples here.
Related
accounts=$(az ad sp list --show-mine --query [].appDisplayName -otsv)
This will give me an array
I want to pass each item to an array in this command
foreach ($myApp in $accounts){
Get-AzureADApplication -Filter "DisplayName eq $myApp"
}
When I try this I get syntax error at position
is this even possible
I'm a noob to powershell
Have you tried enclosing your variable in single quotes?
According to the documentation the value in the filter is quoted:
https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureadapplication?view=azureadps-2.0#example-1-get-an-application-by-display-name
foreach ($myApp in $accounts){
Get-AzureADApplication -Filter "DisplayName eq '$myApp'"
}
I have the following output :
PS> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.8806
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
I'd like to get PSVersion value as a string like this into a variable : 2.0
So I tried this but it's not what I want :
PS> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
and these commands :
EDIT0 : I had copied'n'pasted the wrong line but still the string concatenation didn't work :
PS> $myVariable = $PSVersionTable.PSVersion.Major + "." + $PSVersionTable.PSVersion.Minor
Cannot convert value "." to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:48
+ $myVariable = $PSVersionTable.PSVersion.Major + <<<< "." + $PSVersionTable.PSVersion.Minor
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
PS> $PSVersionTable | Select-Object PSVersion
PSVersion
---------
PS> $PSVersionTable | Get-Item PSVersion
Get-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do
not match any of the parameters that take pipeline input.
At line:1 char:27
+ $PSVersionTable | Get-Item <<<< PSVersion
+ CategoryInfo : InvalidArgument: (System.Collections.Hashtable:Hashtable) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetItemCommand
I'm a powershell rookie, can you help me ?
You can use the .ToString() method on the PSVersion key's value.
$MyVariable = $PSVersionTable.PSVersion.ToString()
Or:
$MyVariable = $PSVersionTable[ "PSVersion" ].ToString()
Note: The different syntax is because $PSVersion is a hash table or a dictionary object. PowerShell lets you use either syntax. Your executing the method on the value that's returned.
$MyVariable will be a string like "2.0". I double checked in version 5.1 and got "5.1.19041.546". If you want to isolate just the major version you can do it like:
$MyVariable = $PSVersionTable.PSVersion.Major
But this won't give you the minor point version.
You could also do something like:
$PSVersionTable.PSVersion.Major,$PSVersionTable.PSVersion.Minor -join "."
This will return "5.1" in my case. So, just major & minor versions but dropping build and revision information. It works by formatting the 2 values into an array and joining them into a string on a "."
Note regarding your question:
The first error is due to PowerShell's type conversion system. This is a little more complex to explain here, but PowerShell will attempt to convert data types to complete operations. It's intuitive but not perfect, and in this case it can't convert the "." to a number. One thing to point out is the tendency to convert the right side to the type of the left. Below would work.
$PSVersionTable.PSVersion.Major.ToString() + "." + $PSVersionTable.PSVersion.Minor
Above manually converts the left to a string so there is no trouble concatenating with ".", when the second "+" is evaluated the Right side is easily converted from [Int32] to [String], so it will work.
In this case I would prefer to use casting like:
[String]$PSVersionTable.PSVersion.Major + "." + $PSVersionTable.PSVersion.Minor
Effectively this is the same, but I prefer it from a syntax perspective.
The select command doesn't work because you are sending the hash table down the pipe and technically there is no "PSVersion" property to select. A correction to that might look something like:
$PSVersionTable.GetEnumerator() |
Where-Object{ $_.Name -eq "PSVersion" } |
Select-Object value
Obviously this is less convenient than other solutions, but I included it for illustration.
The third problem is just not a correct command or syntax. Get-Item cannot take the $PSVerrsionTable hashtable as input, piped or otherwise.
I need to do a case insensitive search in an Excel document using Range.Find.
I'm currently using the following command as an attempt do a case insensitive search for any email address returned by https://haveibeenpwned.com
$Found = $WorkSheet.Cells.Find($SearchText, $null, "xlValues", "xlWhole", "xlByRows", 1, $false) #What, After, Lookin, LookAt, SearchOrder, MatchCase
It returns:
WARNING: [] No public exploits found!
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
At C:\Users\qqqq\Documents\incidents\Search-PwnAddress.ps1:31 char:9
+ $Found = $WorkSheet.Cells.Find($SearchText, $null, "xlValues" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
How do I properly do a Range Find so that I can do a case insensitive search?
From Range.Find - note that xlValues, xlWhole, and xlByRows are not referred to as Strings, but constants. They are members of specific Enumerations - the XlFindLookIn, XlLookAt and XlSearchOrder enumerations.
Enumerations have members with descriptive names that reference a specific value - so in this case, you can use the corresponding value as your argument. Trying to pass their names as Strings - i.e. "xlValues" - will throw a "Type Mismatch" error.
xlValues: -4163
xlWhole: 1
xlByRows: 1
Note also that you should use [Type]::Missing instead of $null, as this answer suggests.
I am storing timestamps in Azure AD User Extension Properties for a custom application.
My extension property is a String data type (available data types are String and Binary).
I am using -format o to get my timestamp string:
[String]$thisTimestamp = (Get-Date -f o)
This gives me a string containing my timestamp formatted as follows:
2017-11-17T18:26:13.5537900+00:00
When I store this string value in my AzureAD Extension Property it appears to go through a String-DateTime-String conversion which results in the default date formatting as follows:
Set-AzureADUserExtension -ObjectId $aadUser.ObjectId -ExtensionName $thisExtensionID -ExtensionValue $thisTimestamp
(Get-AzureADUserExtension -ObjectId $aadUser.ObjectId).get_item($thisExtensionID)
Returns: 17/11/2017 18:26:13
Looking at the stored value in the MS Graph Explorer, it is stored correctly as a timestamp. However, when I Get the value, either with Get-AzureADUserExtension or with Get-AzureADUser and examine the properties, the stored value is always returned as a string, with the format modified:
((Get-AzureADUser -ObjectId $aadUser.ObjectId).extensionproperty).$thisExtensionID
Returns:
17/11/2017 18:26:13
I have a workaround - adding a trailing space to my timestamp. This is correctly stored as a string, and converts back to a DateTime correctly.
[String]$thisTimestamp = (Get-Date -f o) + " "
Set-AzureADUserExtension -ObjectId $aadUser.ObjectId -ExtensionName $thisExtensionID -ExtensionValue $thisTimestamp
(Get-AzureADUserExtension -ObjectId $aadUser.ObjectId).get_item($thisExtensionID)
Returns: 2017-11-17T18:26:13.5537900+00:00 (with a trailing space)
$testDateTime = Get-Date (Get-AzureADUserExtension -ObjectId $aadUser.ObjectId).get_item($thisExtensionID)
Correctly creates a DateTime object and returns:
17 November 2017 18:26:13
It seems wrong to me that the Get-AzureADUser cmdlet and the Get-AzureADUserExtension (which I assume is based on Get-AzureADUser) seem to be interfering with the string as it is retrieved. Is this a bug, or am I doing something stoopid?
I am trying to use the Excel advanced filter through PowerShell, but I am not having any luck. I can use an autofilter successfully by running the following code:
$rangetofilter = $worksheet2.usedrange.select
$excel.selection.autofilter(2, "TestFilter")
However, I don't understand how to properly convert the syntax given in Range.AdvancedFilter Method to something that PowerShell will accept. For example, I've tried
$excel.selection.AdvancedFilter("xlFilterInPlace", "", "", "TRUE")
But I get the following error:
Exception calling "AdvancedFilter" with "4" argument(s): "AdvancedFilter method of
Range class failed"
At line:1 char:32
+ $excel.selection.AdvancedFilter <<<< ("xlFilterInPlace","","","TRUE")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
So is there a way to run an Excel advanced filter through PowerShell?
I found this: Delete duplicate rows in excel using advanced filter, but it is not working either...
None of the arguments to AdvancedFilter() is a string.
Object AdvancedFilter(
XlFilterAction Action,
Object CriteriaRange,
Object CopyToRange,
Object Unique
)
The first is an enumeration. In VBA you can use those directly because there they are implicitly global. Not so in Powershell, where you have to reference them explicitly by their fully qualified names:
$xlFilterInPlace = [Microsoft.Office.Interop.Excel.XlFilterAction]::xlFilterInPlace
The other three arguments are typed as Object, which means they are of Variant type in COM. However, #2 and #3 are supposed to be Range objects, all bets are off if you pass in something else.
They are also marked as optional. Optional parameters that should have no value are represented by the Missing type in .NET COM Interop. Again, in Powershell you have to reference it explicitly:
$missing = [Type]::Missing
Argument #4 is supposed to be a Boolean, so just pass a Powershell bool constant (or, since this parameter is optional as well, $missing).
$excel.Selection.AdvancedFilter($xlFilterInPlace, $missing, $missing, $TRUE)