Archive
Powershell – Restart Azure VM and log off Users remotely
1. Open RUN by pressing Windows + R keys, type powershell command and hit Enter.
2. Now on Powershell window first connect to the Azure VM that you want to remotely restart:
PS C:\Users\manojp> Enter-PSSession -ComputerName MyAzureVMName
[MyAzureVMName]: PS C:\Users\manojp\Documents>
3. Now try issuing the Restart command:
[MyAzureVMName]: PS C:\Users\manojp\Documents> Restart-Computer
Restart-Computer : Failed to restart the computer MyAzureVMName with the following error message: The system shutdown
cannot be initiated because there are other users logged on to the computer.
+ CategoryInfo : OperationStopped: (MyAzureVMName:String) [Restart-Computer], InvalidOperationException
+ FullyQualifiedErrorId : RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComputerCommandSo, this gave us error as few users are still logged in, thus can’t restart the VM.
4. Let’s check who all are logged in on this VM:
[MyAzureVMName]: PS C:\Users\manojp\Documents> quser
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
charlesl rdp-tcp#0 2 Active 1:07 12/21/2018 08:26 AM
5. Let’s try kicking users out by specifying the ID which is “2”:
[MyAzureVMName]: PS C:\Users\manojp\Documents> logoff 2
6. We will check if that user is kicked out or anybody is still remaining:
[MyAzureVMName]: PS C:\Users\manojp\Documents> quser
quser : No User exists for *
+ CategoryInfo : NotSpecified: (No User exists for *:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
7. Let’s finally restart the VM:
[MyAzureVMName]: PS C:\Users\manojp\Documents> Restart-Computer
PS>
Powershell error – Import-Module : File AzureRM.psm1 cannot be loaded because running scripts is disabled on this system
After installing the AzureRM module, I tried to import it, but it gave me an error:
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\AzureRM\4.0.2\AzureRM.psm1 cannot
be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module AzureRM
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleComman
d
This error means that the AzureRM module downloaded from internet must be signed by a trusted publisher before they can be run.
–> Now to fix it you need to change the execution policy by this simple command:
PS c:\> Set-ExecutionPolicy RemoteSigned
If you are on PowerShell Window then it will throw a popup to confirm to change the Execution policy, you can choose “Yes”:
Now try running again the import command, it will run:
PS c:\> Import-Module AzureRM
Powershell error – The term ‘Login-AzureRmAccount’ is not recognized as the name of a cmdlet, function, script file, or operable program
If you are new to PowerShell like me then you would have encountered this ans similar errors.
Recently while executing one PowerShell script to connect to Azure Resource Manager I faced following error:
XyzPSScript.ps1 : The term ‘Login-AzureRmAccount’ 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.
+ CategoryInfo : ObjectNotFound: (Login-AzureRmAccount:String) [XyzPSScript.ps1], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,XyzPSScript.ps1
This error means that you do not have Azure PowerShell module installed on your system or server.
–> So, let’s see how to install the dependent modules.
1. Connect to PowerShell from command prompt (CMD) in Admin mode:
c:\> powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
2. Check the version of PowerShell:
PS c:\> $PSVersionTable.PSVersion
Major Minor Build Revision
—– —– —– ——–
5 1 14409 1005
3. Run the below command to check if you have PowerShellGet installed on your system:
PS c:\> Get-Module PowerShellGet -list | Select-Object Name,Version,Path
Name Version Path
—- ——- —-
PowerShellGet 1.0.0.1 C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PowerShellGet.psd1
4. Now we can easily install the Azure PowerShell from the PowerShell Gallery by runnign following command:
PS C:\> Install-Module AzureRM
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\essensed\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”): YUntrusted 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
On every prompt type ‘Y’ to proceed ahead.
While Installing the module you will see this screen:
5. Finally you just need to import the AzureRM module:
PS C:\> Import-Module AzureRM
This import command will not give any output, and you may proceed with the PS prompt.