ThreadJob module stopped working for seemingly no reason - multithreading

I'm on Powershell 5.1 which IIRC supports ThreadJob OOB.
I have a script which uses the module which used to work without issues.
Now I'm getting the following error:
ForEach-Object : The term 'Start-ThreadJob' 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.
Admittedly I haven't run the script (or used ThreadJob) in a while, but I get the "not recognized" error even when I just call Start-ThreadJob from the terminal.
I've rebooted and run DISM and sfc which didn't find any issues.
This is on a fairly fresh Windows 10 install.
Am I forgetting something?
I vaguely recall experiencing something like this a long time ago, but don't remember how I got around it.

Windows PowerShell - the legacy, Windows-only, ships-with-Windows edition of PowerShell whose latest and last version is 5.1.x - does not come with the ThreadJob module that provides the Start-ThreadJob cmdlet - only the modern, cross-platform, install-on-demand PowerShell (Core) edition (v6+) does.
However, you can install the module on demand in Windows PowerShell, from the PowerShell Gallery, with Install-Module -Scope CurrentUser ThreadJob, for instance.
The following cross-edition idiom installs the module on demand when running in Windows PowerShell, in the scope of the current user:
if ($PSVersionTable.PSVersion -lt 6 -and -not (Get-Command -ErrorAction Ignore -Type Cmdlet Start-ThreadJob)) {
Write-Verbose "Installing module 'ThreadJob' on demand..."
Install-Module -ErrorAction Stop -Scope CurrentUser ThreadJob
}
# Getting here means that Start-ThreadJob is now available.
Get-Command -Type Cmdlet Start-ThreadJob

Related

How to install Az module on Windows Server through DevOps?

I am writing a .Net Core application and in one of the APIs, I have to execute the PS script. Earlier I was using AzureRm commands in my PS script along with .Net Framework which use to work just fine, but after upgrading to .Net Core this script is failing. I updated my PS script to use Az commands but post that am getting an error while executing this script through the API.
Error:
The specified module 'Az' was not loaded because no valid module file
was found in any module directory
DevOps Powershell Script to install Az module:
Install-Module -Name Az -RequiredVersion 2.8.0 -Force -AllowClobber
Get-InstalledModule #Just print out the details to confirm whether `Az 2.8.0` has been installed successfully
// Even setting PSModulePath is not working
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager').OpenSubKey('Environment', $true)
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';D:\Program Files (x86)\ManagedDependencies\PowerShell\AzPSModules\1.0.0'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)
Is there anything else that I need to add to make sure that I can execute Az command in PS script from Azure App Service APIs?
I have tried to install and set the ModulePath by using PowerShell. I am able to Set the ModulePath in the Environment variable.
The workaround follows
# installing the Required Module
Install-Module -Name Az -RequiredVersion 2.8.0 -Force -AllowClobber -SkipPublisherCheck
# here you can get the list of installed Module
Get-InstalledModule
# Set the custom path of PSModulePath in Environment Variable
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager').OpenSubKey('Environment', $true)
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';D:\Program Files (x86)\ManagedDependencies\PowerShell\AzPSModules\1.0.0'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)
The specified module 'Az' was not loaded because no valid module file was found in any module directory
We cannot import all the Az-Modules from the Az folder because Sub-Modules are not installed in the system path. we can load these Sub-Modules one by one.
The sub-modules are automatically being searched in the system path. As they are actually installed in another path, the PowerShell cannot find them, which causes errors. Refer Github discussion for more information
Updated Answer
use the Get-ItemProperty to set the environment path in hosted Agent
# Here using this property in a pipeline
- pwsh: |
$NewPathRegistory = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
Write-Host $NewPathRegistry
Refer here

Trying to use Connect-SPOService in Azure Function

This works fine locally but not when I try to run it in an Azure function. I am assuming that I need to add it as a requirement in the requirements file as I have done with other modules? I spent the last half hour searching but could not figure out how to it this for this particular module (I've done it with others).
** Connect-SPOService -Url https://mycompany-admin.sharepoint.com -Credentials $cred
**
The term 'Connect-SPOService' is not recognized as the name of a cmdlet, function, script file, or operable program. >>
I'll have to do something similar. I haven't tried yet. But maybe, like I do remotely, you have to import the module first, with Windows Compatabilty Mode, since it's a Core PowerShell?
Import-Module -Name Microsoft.Online.SharePoint.PowerShell -UseWindowsPowerShell

Installing Azure Module on an AWS instance using userdata

I'm trying to automatically install the Azure module for PowerShell (Az) on an AWS EC2 instance through userdata so Azure commands can be executed when an instance is started. However, I'm getting this error:
PackageManagement\Install-Package : Cannot convert value
"2.0.0-preview" to type "System.Version". Error: "Input string was
not in a correct format."
Other people who have reported this problem have solved it by upgrading PowerShellGet and removing the old version. I'm doing that with this block of code:
Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force -Confirm:$false
Install-Module -Name PowerShellGet -Repository PSGallery -Force -Confirm:$false
Remove-Item -Path "C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1" -Force -Recurse
However when I run the command to install the moudule after that:
Install-Module -Name Az -AllowClobber -Force -Confirm:$false -Scope AllUsers
I still get the same error.
How can I fix this without having to close and reopen the shell? This all needs to run as part of the same script.
You used Remove-Item, however that command just deleted the module file. The imported module still existed in your PS session. So, you may try to use Remove-Module remove it and use Import-Module to import the right one. But, this is not the suggested way, you may try the following two approaches:
You can just use Update-Module -Name PowerShellGet -Force to directly update your existing module.
Another way is to uninstall your old module, and then install the new version.

Issues with installing Azure specific PowerShell modules for writing to Azure Tables

We are currently rolling out Windows 10 (1903) devices via Intune, and we have requirements to install Azure PowerShell modules under the System account for the devices to communicate and write data to Azure tables. There is a script running under the system account that will write data to Azure every so often.
None of these modules are loading, even when we manually copy them across to the correct location and try the 'offline' method, and we have noticed the following errors on the clients within Event Viewer:
Cannot convert value "2.0.0-preview" to type "System.Version". Error: "Input string was not in a correct format."
The modules that I am trying to install are:
Install-Module -Name Az -AllowClobber -Force | Out-Null
Install-Module -Name AzureRmStorageTable -RequiredVersion 2.0.1 -Force | Out-Null
Any advice/help would be greatly appreciated.
Referring to Github issue: Can't install PowerShell Az - Cannot convert value "2.0.0-preview" Windows 10
If your PowerShellGet module version is less than 1.6.0, you might encounter this issue. You may update the PowerShellGet module nad have another try.

The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program

Created Powershell Azure function and trying to use "az" commands under that function app. As per docs, function runtime should resolve "az" and other module dependencies. but it doesn't work for me.
ERROR: The term 'az' 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.
Microsoft.Azure.WebJobs.Script.Rpc.RpcException : Result: ERROR: The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program.
I want to run some "az" command under function app without manually uploading modules. Is it powershell Preview version issue or something I need to correct?
requirement.psd1
#{
Az = '2.*'
}
For those who get these error in local while trying to access AZ, try this command below in an admin PowerShell instance.
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
You can get more details about installation # https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-powershell
Make sure you restart the powershell instance
"Az" in the context of PowerShell probably means the Az module, with cmdlets like Add-AzAccount etc.
"az" is the cross-platform CLI, which is not a PowerShell module.
I got this error because I had not installed CLI. I was sent this link which downloaded the required install:
https://aka.ms/installazurecliwindows
Two ways you can solve this issue.
Install below installation file for Azure CLI in windows.
https://aka.ms/installazurecliwindows
(OR)
Install using PowerShell the below command.
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
More details: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-cli
I can see PowerShell 7.x and later is the recommended version.
You can check Powershell version using below command
$PSVersionTable.PSVersion
https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-6.2.0
NOTE
PowerShell 7.x and later is the recommended version of PowerShell for
use with the Azure Az PowerShell module on all platforms.
Run this to fix:
Install-Module AzureAD -Force
Install-module AzureADPreview -Force
Install-Module -Name MSOnline -Force
Import-Module Az -Force
Install-Module Az -Force
Download this then this problem will not occur.
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli

Resources