Intune Autopilot Powershell Script as an win32 app Error only in autopilot

209 views Asked by At

I try to install a Programm with a Powershell Script ober Intune Autopilot. This Script runs into an Error only in Autopilot. It can be run ober the company Portal or with right click run with powershell.

The Script installs the Module Microsoft Graph to connect to azure and read some data.

But when the script is run over autopilot in user context i get following error:

original:
Get-Command : Die Benennung "Connect-MgGraph" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder 
eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern 
enthalten), und wiederholen Sie den Vorgang.Get-Command : Die Benennung "Connect-MgGraph" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder 
eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern 
enthalten), und wiederholen Sie den Vorgang.

translated:
Get-Command: The naming "Connect-MgGraph" was not used as the name of a cmdlet, function, script file, or
of an executable program. Check the spelling of the name or whether the path is correct (if
included), and repeat the operation.Get-Command: The naming "Connect-MgGraph" was not used as the name of a cmdlet, function, script file, or
of an executable program. Check the spelling of the name or whether the path is correct (if
included) and repeat the process.

The Errorcode from powershell is the following:

original:
Es ist nicht möglich, einen Index auf ein NULL-Array anzuwenden.

translated:
It is not possible to apply an index to a NULL array.

The follwoing Code is used to get the Error:

$targetParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']

if ($targetParameter.ParameterType -eq [securestring]){
    Connect-MgGraph -AccessToken ($token |ConvertTo-SecureString -AsPlainText -Force)
}
else {
    Connect-MgGraph -AccessToken $token
}

I tried multiple Versions from the Grapg Api because of that I set the requiered Version on 2.5.

The Script runs over COmpany Portal and with right click run with powershell.

1

There are 1 answers

2
opsi-se On BEST ANSWER

I found the solution:

I had installed my modules with -Scope CurrentUser.
Because of that, Connect-MgGraph couldn't be used with Autopilot.

I removed the -Scope argument and now it works, though I'm unclear on why, given that it worked in other invocation scenarios.