PowerShell Invoke-AzOperationalInsightsQuery Error - windows

When trying to use Invoke-AzOperationalInsightsQuery I get the following error
Invoke-AzOperationalInsightsQuery: The term 'Invoke-AzOperationalInsightsQuery' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I am not too sure on how to fix this issue, any help is welcome, thanks.

Invoke-AzOperationalInsightsQuery Cmdlet is part of Az PowerShell module and not part of default PowerShell environment. You can install it via Install-Module Cmdlet. Or using MSI package installer.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

Related

How to install a powershell module inside a docker container using a docker file

I have a basic powershell script which makes changes to SharePoint online (Office365). This task requires Microsoft.Online.SharePoint.PowerShell module to be installed in the system where the script is to be executed.
Below is my docker file:
FROM mcr.microsoft.com/powershell:latest
WORKDIR ./
CMD pwsh -Command Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser -Force
COPY SampleScript.ps1 ./
CMD pwsh .\SampleScript.ps1
when I'm running:
docker run --name "pwsh-demo" --interactive --tty dockerdemo
I'm not able to get into interactive mode. But when I'm trying to enter the container manually and installing the module manually without using dockerfile, then the module is installing but when I'm running the script, it's throwing error saying:
The term 'Connect-SPOService' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is
| correct and try again.
Though the module is installed, I verified using the below command:
Get-Module -ListAvailable

Running a psm1 PowerShell script in Command Prompt (cmd.exe)?

I know nothing of PowerShell, but I wanted to install this: https://www.powershellgallery.com/packages/lolcat/
So, I start PowerShell as administrator, and:
PS C:\WINDOWS\system32> Install-Module -Name lolcat NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Me\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import
the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y
PS C:\WINDOWS\system32> lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...
Nice, it works. So first I find where is the newly installed script:
PS C:\WINDOWS\system32> (Get-Module lolcat).Path
C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1
Ok, so now I want to try calling this from cmd.exe:
C:\Users>PowerShell.exe -File "C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1"
Processing -File 'C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
Nope, does not work.
Is it possible to call this PowerShell script from cmd.exe - and if so, how?
The error is due to the fact that the -File parameter from the powershell.exe excepts a .ps1 file.
If you want to run C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1 from cmd, make a .ps1 script where you can write something like
Import-Module lolcat
# now you have all the functions from the lolcat module loaded into this PowerShell session
# do stuff
And then call this script from cmd.
The difference between .ps1 and .psm1 is explained here.
Eh, turned out to be less complicated then I thought - since it is a "module", just use the module name:
C:\Users>PowerShell.exe lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...

Cannot run Remove-IISSite inside a windows docker container

Yo fellow stackoverflow-ers!
I've recently hit a snag when trying to create a windows container that runs IIS. Currently, my dockerfile looks like the following
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-ltsc2016
# Install Powershell
ADD https://github.com/PowerShell/PowerShell/releases/download/v7.0.0/PowerShell-7.0.0-win-x64.zip c:/powershell.zip
RUN powershell.exe -Command Expand-Archive c:/powershell.zip c:/PS7 ; Remove-Item c:/powershell.zip
RUN C:/PS7/pwsh.EXE -Command C:/PS7/Install-PowerShellRemoting.ps1
# Update shell to powershell (PS7)
SHELL ["C:/PS7/pwsh.EXE", "-command"]
# Install chocolatey
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# Install application dependencies via chocolatey
RUN choco install -y vcredist140
RUN choco install -y nuget.commandline
# Enable required IIS features
RUN Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebSockets;
RUN Enable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationInit;
RUN Enable-WindowsOptionalFeature -Online -FeatureName IIS-BasicAuthentication;
RUN Enable-WindowsOptionalFeature -Online -FeatureName IIS-WindowsAuthentication;
RUN Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpCompressionDynamic;
# Install IISAdministration to manage IIS configuration
RUN Install-Module -Name IISAdministration -Force -MinimumVersion "1.1.0.0";
# Remove default web site
RUN Remove-IISSite -Name 'Default Web Site'
This results in the following error when the last command is called
Remove-IISSite: The term 'Remove-IISSite' 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.
Note: I'm using PS7, as the current Powershell version that comes with the above container doesn't allow me to install IISAdministration (which is slightly annoying, but hey-ho! Probably something I'm doing wrong).
Any help and/or advice on the current situation would be greatly appreciated!
As per your comment, I have had issues specifically on Server Core 2016 too with PowerShell Modules, so can sympathise. The IISAdministration module has not been ported to .NET Core. You would have to use the current Desktop version of PowerShell i.e. 5.1.
The simplest solution is to use Import-Module IISAdministration -UseWindowsPowerShell instead of Install-Module -Name IISAdministration.
That's how PowerShell 7 loads old modules in the compatible way (a remote session to 5.1),
Reference

Azure module cannot be loaded in Mac OS

When I try to connect to azure:
Connect-AzureRmAccount
Getting following error:
Connect-AzureRmAccount : The 'Connect-AzureRmAccount' command was found in the module 'AzureRM.Profile.Netcore', but the module could not be loaded. For more information, run 'Import-Module AzureRM.Profile.Netcore'.
But when trying to Import-Module AzureRM.Profile.Netcore, it gives me this error:
Import-Module : Could not find a part of the path '/Users/Gurnor/.local/share/powershell/Modules/AzureRM.Resources/6.4.0/Microsoft.Azure.Commands.Resources.format.ps1xml'.
Googled above error but cant find any answers? Can anyone please help?
I had a similar problem and solved it by uninstalling the AzureRM module(s) and switching to the new Az modules.
In pwsh, run: Uninstall-AzureRm
Then: Install-Module -Name Az -AllowClobber -Scope CurrentUser
If you run Enable-AzureRmAlias, most scripts using "AzureRm" cmdlets should work.
If you still have issues, try running Get-InstalledModule -Name AzureRM -AllVersions and see if you need to uninstall more modules, I think my problem was that between brew and terminal and vscode, I had several versions installed. I was only able to get things working smoothly by eliminating everything except the Az module.
https://learn.microsoft.com/en-us/powershell/module/az.accounts/uninstall-azurerm?view=azps-2.7.0
https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-2.7.0
https://learn.microsoft.com/en-us/powershell/azure/migrate-from-azurerm-to-az?view=azps-2.7.0

New-SShSession command not found in jenkins powershell

I'm using Windows Jenkins and installed the windows powershell plugin. from Jenkins I'm trying to connect to Linux instance and execute some commands in linux server.
> New-SshSession -ComputerName 10.0.0.xx -Username username-Password
> jenkins#123 Invoke-SshCommand -ComputerName 10.0.0.xx -Command "cd
> docker_CIServiceApp ; unzip prod.zip -d prod/"
But getting the error message from jenkins job as
New-SshSession : The term 'New-SshSession' 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.
The same command works in Windows Powershell terminal. What will be the issue?
The reason for it might be that the Jenkins was started before SSH.NET plugin was installed. It modifies the environment variable and it requires process restart. Try restarting Jenkins process. Alternatively, it might be that the module is not loaded, try loading it first: Import-Module SSH-Sessions.
So, apparently you have installed a package in Visual Studio solution. Visual studio takes care of package download for you (restoration). It gets downloaded into packages folder under your solution. When you have no Visual Studio, like when using Jenkins and MS Build directly, you have to download package manually - get nuget.exe and run nuget restore your_solution.sln, this will then download the package under "\packages". Now you will have to import module, using the "\packages" path, probably.
your error indicates tha the module isn't installed:
PS /home/thufir/powershell>
PS /home/thufir/powershell> Install-Module -Name SSHSessions
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this
repository, change its InstallationPolicy value by running the Set-PSRepository
cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "N"):Yes
PS /home/thufir/powershell>

Resources