unattend-generator icon indicating copy to clipboard operation
unattend-generator copied to clipboard

Usage of Get-WindowsAutoPilotInfo

Open bokkoman opened this issue 1 year ago • 16 comments

Hi,

I want to use this script (https://www.powershellgallery.com/packages/Get-WindowsAutoPilotInfo/3.8) before windows user creation is starting, so when windows is in the Getting Started fase (on your website its called: Scripts to run in the system context, before user accounts are created).

This is so an installation of Windows automatically registers the laptop to Microsoft Intune. Do you think that is possible, i have it working with another script, but I rather use the official one.

Thanks!

bokkoman avatar Aug 06 '24 10:08 bokkoman

I have no experience with Intune. Your custom script would probably look like this:

Install-Script -Name Get-WindowsAutoPilotInfo -RequiredVersion 3.8
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv

I would first try to add this in the Scripts to run in the system context, before user accounts are created section. If this doesn't work, add it to the Scripts to run when the first user logs on after Windows has been installed section. In any case, check the log files at C:\Windows\Setup\Scripts\unattend*.log.

cschneegans avatar Aug 06 '24 11:08 cschneegans

This needs to run before any user ever touches the laptop. Would I put that command and run it as cmd or ps1? I am assuming ps1, cause those are powershell commands.

bokkoman avatar Aug 06 '24 11:08 bokkoman

Another note, when I added this command as run as cmd;

FOR %i IN (C D E F G H I J K L N M O P Q R S T U V W X Y Z) DO IF EXIST %i:\AutoPilot.ps1 powershell -ExecutionPolicy Bypass -File %i:\AutoPilot.ps1 -AuthFile %i:\Auth.ps1 -GroupTag Test

It doesn't work, because the % are removed.

bokkoman avatar Aug 06 '24 12:08 bokkoman

It doesn't work, because the % are removed.

In .cmd files, you need to write %% instead of %.

cschneegans avatar Aug 06 '24 12:08 cschneegans

It doesn't work, because the % are removed.

In .cmd files, you need to write %% instead of %.

That's funny, cause this works fine:

        <RunSynchronous>
          <RunSynchronousCommand wcm:action="add">
            <Order>1</Order>
            <Path>cmd /q /c "FOR %i IN (C D E F G H I J K L N M O P Q R S T U V W X Y Z) DO IF EXIST %i:\AutoPilot.ps1 powershell -ExecutionPolicy Bypass -File %i:\AutoPilot.ps1 -AuthFile %i:\Auth.ps1 -GroupTag Test"</Path>
            <Description>Run AutoPilot script</Description>
          </RunSynchronousCommand>

bokkoman avatar Aug 06 '24 12:08 bokkoman

Not a surprise here. In your example, there is no .cmd file. But with the way my generator stores and runs custom scripts, there is.

cschneegans avatar Aug 06 '24 12:08 cschneegans

Ok i tested your command, it did not work, this is the logs;

The variable '$script:PSGetSettingsFilePath' cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6664 char:60
+ ... .PowerShell.Management\Test-Path -Path $script:PSGetSettingsFilePath)
+                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (script:PSGetSettingsFilePath:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined
 
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6547 char:5
+     $AlreadyPromptedForScope = $script:PSGetSettings.Contains($scopeS ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
The variable '$script:PSGetSettingsFilePath' cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6664 char:60
+ ... .PowerShell.Management\Test-Path -Path $script:PSGetSettingsFilePath)
+                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (script:PSGetSettingsFilePath:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined
 
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6555 char:9
+         $AlreadyPromptedForScope = $script:PSGetSettings.Contains($sc ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Cannot index into a null array.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6629 char:9
+         $script:PSGetSettings[$scopeSpecificKey] = $userResponse
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
 
WARNING: The property 'Values' cannot be found on this object. Verify that the property exists.
WARNING: The property 'Keys' cannot be found on this object. Verify that the property exists.
WARNING: The variable '$script:PSGetModuleSourcesFilePath' cannot be retrieved because it has not been set.
PackageManagement\Install-Package : No match was found for the specified search criteria and script name 
'Get-WindowsAutoPilotInfo'. Try Get-PSRepository to see all available registered script repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:3538 char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex 
   ception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
 
.\Get-WindowsAutoPilotInfo.ps1 : The term '.\Get-WindowsAutoPilotInfo.ps1' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
path is correct and try again.
At line:2 char:1
+ .\Get-WindowsAutoPilotInfo.ps1 -GroupTag "Test" -Online -TenantId ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\Get-WindowsAutoPilotInfo.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

bokkoman avatar Aug 06 '24 13:08 bokkoman

It appears that the Install-Script cmdlet does not like to be run in the system context. This approach worked better for me:

  1. Copy the entire script code and paste it to a custom .ps1 script.
  2. Prepend a line & {.
  3. Append a line } -OutputFile "C:\Windows\Setup\Scripts\MyComputer.csv";.

This URL configures the form accordingly.

cschneegans avatar Aug 06 '24 13:08 cschneegans

Thanks I will try that tomorrow. Though, I do not want to output it to a csv file, I want to use the Online method. I will just edit the params in the script to default to the strings i need.

In normal usage, I would run the script like this;

Get-WindowsAutoPilotInfo.ps1 -Force -GroupTag "Test" -Online -TenantId "XXXXXX" -AppId "XXXXXXX" -AppSecret "XXXXX" -Reboot

bokkoman avatar Aug 06 '24 13:08 bokkoman

Oh, and i think the part that fails is the Nuget installation?

bokkoman avatar Aug 06 '24 14:08 bokkoman

@bokkoman

This is what it would need to look like to fix the nuget issue, I'm not sure if AppSecret still works or if its been replaced with certificate authentication.

Set-ExecutionPolicy Bypass -Scope Process -Force; Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force; Install-Script -Name Get-WindowsAutoPilotInfo -Force; Get-WindowsAutoPilotInfo -Online -TenantId "123" -AppId "123" -AppSecret "123" -Force -Reboot

Jheesbrough avatar Aug 13 '24 14:08 Jheesbrough

@bokkoman

This is what it would need to look like to fix the nuget issue, I'm not sure if AppSecret still works or if its been replaced with certificate authentication.

Set-ExecutionPolicy Bypass -Scope Process -Force; Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force; Install-Script -Name Get-WindowsAutoPilotInfo -Force; Get-WindowsAutoPilotInfo -Online -TenantId "123" -AppId "123" -AppSecret "123" -Force -Reboot

Thanks for the feedback. I did some heavy editing in the powershell script of the Get-WindowsAutoPilotInfo. It needed to be online only and appid only, so the installation is fully automated. I had to get invoke requests from the modules needed and put them into the script as functions, that way it doesn't rely on nuget/module installation. Removed everything not needed and it works like a charm with the old command I was using.

cmd /q /c "FOR %i IN (C D E F G H I J K L N M O P Q R S T U V W X Y Z) DO IF EXIST %i:\AutoPilot.ps1 powershell -ExecutionPolicy Bypass -File %i:\AutoPilot.ps1 -Settings%i:\Settings.ps1"

Also made it use a Settings.ps1 with all the variables that is loaded.

Now I can fully unattend an installation for a laptop at work.

bokkoman avatar Aug 13 '24 14:08 bokkoman

Would you be able to share the script somewhere so I can compare? I have a similar setup and would be great to have a look as it sounds like your version works well!

Jheesbrough avatar Aug 13 '24 14:08 Jheesbrough

@Jheesbrough I created a repo; https://github.com/bokkoman/IntuneAutoPilot

bokkoman avatar Aug 14 '24 11:08 bokkoman

That's great! I've made a similar one which puts the whole script in the XML file but it appears to be janky, I'll put up soon, the additional WiFi looks great.

Jheesbrough avatar Aug 14 '24 11:08 Jheesbrough

That's great! I've made a similar one which puts the whole script in the XML file but it appears to be janky, I'll put up soon, the additional WiFi looks great.

For the WiFi, it can also be janky if your laptop does not work with default WiFi drivers. That's why I just download many drivers for the types of laptops we have, and put them into the ISO (i use NTlite for that, but DISM should work as well).

bokkoman avatar Aug 14 '24 11:08 bokkoman