How to find all VM/Hyper-V and then reboot them on Window Server 2012 with the use of Power-shell - powershell

I would like to ask how to find all VM/Hyper-V on Window Server 2012 with the use of power-shell and then restart/reboot them with the use of power-shell script?
The next thing is:
I cannot even find any get command for it:
==================
Added in 2014-08-15
have find a reference link that help me a lot, just share here
http://technet.microsoft.com/en-us/library/hh848479.aspx

Run Powershell elevated (!) and execute following:
ForEach ($VM in Hyper-V\Get-VM | Where {$_.State -ne "Off") {
Restart-Computer -ComputerName $VM.Name
}
Non-elevated Powershell will not return any results.
Martin

Related

How do I start and stop several IIS Applicationpools at once with a PowerShell script

I would like to create a PowerShell script that can Start and Stop several IIS Applicationpools at once.
I already found a similar article about this: How to start and stop application pool in IIS using powershell script
But I would like to create a PowerShell script where I can define more than one IIS Application to stop them all at once through that script.
Thank you in advance for the help
The link you provided already has a solution very close to what you need; look for "Stop all application pools script".
If you want to start/stop only some AppPools, put them in an array: replace $AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Started"} with $AppPools=#('server1', 'server2', 'server3'):
$AppPools=#('server1', 'server2', 'server3')
ForEach($AppPool in $AppPools) {
Stop-WebAppPool -name $AppPool.name
}

Check existence of single role in server 2016 using powershell

I'm trying to check if Windows Deployment Services is installed in server 2016 using powershell and then use this condition to do further steps. I've tried using Get-WindowsFeature but it gives the status list of all roles and features. I want a command that checks whether a single role or feature is installed or not.
My intention is to:
if(WDS is not installed){
Install-WindowsFeature -Name WDS }
else
Do nothing
Facing problem in finding out status of WDS role
Found the answer thanks to #TheIncorrigible1 and #DavidMartin
Using Get-WindowsFeature -Name WDS | % Installed worked.
Also, Get-WindowsFeature -Name WDS | Format-List helps in finding more helpful details.
You can use
param(
[Parameter(Mandatory=$true)][string]$FeatureName
)
(get-windowsfeature |where name -eq $FeatureName).Installstate
Just pass FeatureName to the the variable

How can I find the Microsoft Edge version on Windows 10 in powershell?

I've searched through the SOFTWARE\Classes and SOFTWARE\Microsoft subkeys, but couldn't find anything related to "spartan" or "edge". Given that Edge is still very new, there really isn't much information about this yet.
As an example, this is how I gather the information about the Internet Explorer Version via the registry on a remote machine:
$ieVersion=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $args[0]).OpenSubKey('SOFTWARE\Microsoft\Internet Explorer').GetValue('SvcVersion')
Use the Get-AppxPackage command:
Get-AppxPackage -Name Microsoft.MicrosoftEdge | Foreach Version
The package name is valid on build 10240 but if you are on an earlier build, it might be different. If the above doesn't find the package try -Name *Edge* or -Name *Spartan*.
$productPath = $Env:WinDir + "\SystemApps\Microsoft.MicrosoftEdge_*\MicrosoftEdge.exe"
If(Test-Path $productPath) {
$productProperty = Get-ItemProperty -Path $productPath
Write-Host $productProperty.VersionInfo.ProductVersion
}
Else {
Write-Host "Not find Microsoft Edge."
}
Source How to determine the version of Microsoft Edge browser by PowerShell
For Edge on Chromium above 44 version
powershell:
Get-AppxPackage -Name *MicrosoftEdge.* | Foreach Version
cmd:
powershell "Get-AppxPackage -Name *MicrosoftEdge.* | Foreach Version"
I tested using two commands back to back. I ran from an elevated PowerShell session New-PSSession -ComputerName "The remote PC I was testing this on." and then once the connection was made I ran the Get-AppxPackage -Name Microsoft.MicsrosoftEdge and it pulled down the information, but I think it was more build information. You can also filter it down to version using the Pipe character. The full command looked like this.
Get-AppxPackage -Name Microsoft.MicrosoftEdge | select-object Version
I found this forum and others that lead me to some of the other switches and parameters I did not know about.
How can I find the Microsoft Edge version on Windows 10 in powershell?
I was trying to find an alternate way of remotely finding out what browser version it was. Trying to verify if it is updating regularly. I am still learning. I hope this helps. I found another article that shows me exactly what I am looking for differently without powershell.
https://www.tenforums.com/tutorials/161325-how-find-version-microsoft-edge-chromium-installed.html#option2
You may see multiple versions of Edge installed via Appx-Packages. I would recommend this approach:
$EdgeExe = Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe' "(default)"
$version = (Get-Item $EdgeExe).VersionInfo.ProductVersion

Command not found when execute powershell script from IIS

I have a script that gets the citrix sessions for a relation on our platform.
The problem is that when I run the script from the console on the webserver, the executing of the script is succesfull and the result is as expected.
When I run the script from my website (hosted on IIS) the result is as follows:
"The term 'Get-XASession' is not recognized as the name of a cmdlet, function, script file, or operable program."
This is the script:
#----------------------------------------------------------
# PARAMS TO CALL SCRIPT WITH
#----------------------------------------------------------
Param(
[string]$relationId,
[string]$xaVersion,
[string]$xaServer
)
#----------------------------------------------------------
# LOAD ASSEMBLIES AND MODULES
#----------------------------------------------------------
try
{
Import-Module "C:\Program Files\Citrix\XenApp 6.5 Server SDK\Citrix.XenApp.Sdk.ps1"
. "D:\scripts\include\functions.ps1"
}
catch
{
return "[ERROR]`t Import module XenApp 6.5 Server SDK gives an error $($_.Exception)"
}
#----------------------------------------------------------
#START
#----------------------------------------------------------
Set-ExecutionPolicy Unrestricted
try
{
if ($xaVersion -eq "XA65")
{
$sessions = Get-XASession -ComputerName $xaServer | Where-Object { $_.AccountName -like "ASPECT\$relationId*" }
$listSessions = #()
foreach($se in $sessions)
{
New-Object psobject -Property #{AccountName = $se.AccountName; SessionId = $se.SessionId; Name = $se.BrowserName; ClientName = $se.ClientName; ServerName = $se.ServerName; LogonTime = $se.LogonTime; Status = $se.State}
}
return $listSessions
}
}
catch
{
return "[ERROR]`t Sessies ophalen voor gebruiker $samAccountName met XAversie $xaVersion gives the following error $($_.Exception)"
}
The identity of the IIS Application Pool is the same as the identity that I used to run the script from the console.
That user has all the rights it needs to access the Citrix XenApp module.
The wierd thing is, when I say Get-Command Get-XASession -neq $null then the script says, hé I know that command lets go to execute it. When it is going to execute it then the sripts says, huh Get-XASession? never hurt of.
I spent hours and hours and I don't have a clue.
Please help me!
jisaak did put me in the right direction.
Dot sourcing could be the answer but that gives an error about user interaction.
This is wat I did: I opened the module and copied everything except the write-host lines into my script. I did run it and guess what, it works!
So if you experience the same problem as me:
1. try dot sourcing
2. try to copy the content of the module into your script.
All credits go to jisaak (Y) :)

Return code and status from PowerShell command

I'm running the following command from within a Microsoft System Centre Orchestrator PowerShell activity:
Install-WindowsFeature -ConfigurationFilePath C:\DeploymentConfigTemplate.xml -ComputerName ServerXYZ
the command isn't doing what it's supposed to do, and I want to be able to return if the command was successful or not, and any error message if possible. Ignore the fact it's running in Orchestrator, as I'm more concerned about the PowerShell question. When I run the command from ISE it does what it's supposed to do, that's why I want to see what is returned from PowerShell.
Thanks.
It's hard to know what may be happening without more context. The following will record any errors encountered in an xml file that you can import later with import-clixml:
Install-WindowsFeature -ConfigurationFilePath C:\DeploymentConfigTemplate.xml -ComputerName ServerXYZ
IF (!($?)) {
$error[0] | export-clixml C:\myerror.xml
}
This solves my problem:
$Result = Install-WindowsFeature -Name SNMP-Service -IncludeAllSubFeature -IncludeManagementTools
Write-Host $Result.ExitCode