Finding Version of IIS using powershell - iis

I wish to find the version of IIS using a powershell (or WMI) query.
The following is the query which I have used.
Get-WmiObject -namespace "root\microsoftiisv2" -query "select MajorIIsVersionNumber from IISWebInfo"
I tested this query using powershell console in a 'Windows 8' PC with 'IIS 8'. But the result is 7 , where the expected version Number is 8.
Can someone help me to solve this issue?.

You can try:
get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\ | select setupstring,versionstring

Even though the thread is little old,this is the link I landed first. So letting you know what I found.
The below command helped me find the IIS version correctly on IIS 8.5 (Windows 2012 R2) and 7.5 Windows 7 SP1.
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$env:SystemRoot\system32\inetsrv\InetMgr.exe").ProductVersion
Reference:
https://forums.iis.net/p/1171695/1984536.aspx : Answer from f00_beard

If you want the decimal value for order comparison.
$iisInfo = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\InetStp\
$version = [decimal]"$($iisInfo.MajorVersion).$($iisInfo.MinorVersion)"

Here's a little ScriptBlock function that I created based on the answer from #C.B. to get the IIS Version from a remote computer.
$pwd = convertto-securestring "yourstrongpasswordhere" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist "machinenamehere\adminusernamehere",$pwd
$iisversion= Invoke-Command -ComputerName $machineName -ScriptBlock {
$(get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\).setupstring
} -Credential $cred
Write-Host iisversion = $iisversion
If($iisversion -like '*IIS 6*'){
Write-Host This server uses IIS6
}
If($iisversion -like '*IIS 7*'){
Write-Host This server uses IIS7
}

For purely the version, I prefer checking the info on the w3wp executable.
Per the "how-to" article from MSFT:
If(Test-Path $w3wpPath) {
$productProperty = Get-ItemProperty -Path $w3wpPath
Write-Host $productProperty.VersionInfo.ProductVersion
}
Else {
Write-Host "Not find IIS."
}

Related

Azure Automation Hybrid-Worker Get-AutomationPSCredential for PowerShell 7

We are moving a few Azure Automation Hybrid-worker scripts to PowerShell 7.1. In doing so one of the commands that work in PowerShell 5.1 is: [PSCredential] $AutomationCredential = Get-AutomationPSCredential -Name 'abcdef'. When we try the same command in PowerShell 7.1 we get an error The 'Get-AutomationPSCredential' command was found in the module 'Orchestrator.AssetManagement.Cmdlets', but the module could not be loaded. For more information, run 'Import-Module Orchestrator.AssetManagement.Cmdlets'
We have added the Import-Module to the code but we get Could not load file or assembly 'JobRuntimeData.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
We do find Orchestrator.AssetManagement.Cmdlets the on the hybrid-worker, in the sandbox area. We know that this module is loaded when the hybrid-worker is installed (https://learn.microsoft.com/en-us/azure/automation/shared-resources/modules#internal-cmdlets).
Based on the Microsoft official document
The 'Get-AutomationPSCredential is currently supported till Azure powershell 6.6.0
NOTE: Powershell 7.1 is still in preview so this may be the reason for the error .
Please refer this MS DOC for more information.
We are assuming the Orchestrator.AssetManagement.Cmdlets is bugged at this point, but have not found anything to say that it is. To get around it we were going to use a runbook variable to store the password, but it needs the cmdlet to actually decode a secret/hidden value.
In the end, we used a key vault to store the password. The following is what was used as a workaround.
$User = "AutomationUser"
$KeyVaultName = "KeyVault"
try {
$Password = (ConvertTo-SecureString (Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $User -AsPlainText) -AsPlainText -Force)
[PSCredential] $AutomationCredential = New-Object System.Management.Automation.PSCredential($User, $Password)
}
catch {
$ErrorMessage = "Unable to retrieve credentials for user: [${$User}]. $_"
throw $ErrorMessage
BREAK
}

How do you configuring Azure VMs with .NET 5.0 from the command line?

I'm setting up some virtual machines to run my service. There may be several, so I'm trying to automate the process. I've got a PowerShell script that successfully build the virtual machine, but now I want to install the dependent software that my .NET Core Web Application requires in the same script.
The first dependency I want to install is .NET 5.0 Runtime. I've done this many times from the browser, but now I want to commit this to a script that runs after the VM has been built.
Test locally or on a test VM by installing using the dotnet-install-script and finalize the parameters. Then use Set-AzVMExtension to install that script using custom script extension. The code would look like this (not tested)
$Command = "&powershell -NoProfile -ExecutionPolicy unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) <additional install-script args>"
$Settings = #{"commandToExecute" = "Powershell $Command";};
Set-AzVMExtension `
-ResourceGroupName "ResourceGroupName" `
-Location "Location" `
-VMName "VirtualMachineName" `
-Name "ExtensionName" `
-Publisher "Contoso.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion "1.1" `
-Settings $Settings
Full details and schema of settings custom-script-windows
You can also use Set-AzureVMCustomScriptExtension for running custom scripts.
As #amit_g recommended, you can use Azure VM run command functionality to run install .net with scripts by PowerShell directly to meet your requirement.
This seems to work pretty well:
Invoke-AzureRmVMRunCommand -ResourceGroupName "$resourceGroupName" -Name "$machineName" -CommandId "RunPowerShellScript" -ScriptPath "configureMachine.ps1" -Parameter #{"machineName" = "$machineName"}
The contents of the Powershell script look something like this:
# The name of the VM is passed in as the first parameter.
param ($machineName)
if ($machineName -eq $null)
{
Write-Host "Usage: configureMachine -machineName <machineName>";
Exit;
}
# Download the agent installation files.
$agentZip="agent.zip";
Invoke-WebRequest -Uri "https://vstsagentpackage.azureedge.net/agent/2.181.1/vsts-agent-win-x64-2.181.1.zip" -OutFile $agentZip
# Unpack them.
$agentDirectory="$env:SystemDrive\azagent";
Add-Type -AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, $agentDirectory);
# Configure the machine to work as a DevOps Agent.
&"$agentDirectory\config.cmd" --unattended --deploymentgroup --deploymentgroupname "Production" --agent "$machineName" --runasservice --work "_work" --url "https://dev.azure.com/theta-rex/" --projectname "openbook" --auth PAT --token te64yuv36tina2rvc2lsvwcsvctpwomiewz5fxihcubbdzaasoka
# Remove the Agent Zip files when installation is complete.
Remove-Item $agentZip;
# Download and install .NET 5.0
Invoke-WebRequest -Uri "https://dot.net/v1/dotnet-install.ps1" -OutFile "dotnet-install.ps1"
&"./dotnet-install.ps1" -Channel 5.0 -Runtime aspnetcore -InstallDir "C:\Program Files\dotnet"
The end result is a machine configured to participate in Azure DevOps and ASP.NET 5.0.

How to create a Microsoft Team using a Queue Triggered Azure Function writed in PowerShell?

I want to create a Team by using an azure function triggered by an Azure Queue.
Unfortunetly when I run the code it is not working inside the Azure Function.
I'm wondering. Is there a way to create a Microsoft Team using PowerShell inside an Azure Function ?
Import-module MicrosoftTeams
$group = New-Team -MailNickname "teamTitle" -displayname "teamTitle" -Visibility "private"
Add-TeamUser -GroupId $group.GroupId -User "user#etc.com"
New-TeamChannel -GroupId $group.GroupId -DisplayName "General"
Working locally. Not working within the Azure Function.
Bellow the error i'm getting :
ERROR: Import-Module : The specified module 'MicrosoftTeams' was not loaded because no valid
module file was found in any module directory. At D:\home\site\wwwroot\CreateTeam\run.ps1:3
char:1 + Import-Module MicrosoftTeams + [...]
Thank you
Based on the error message, your Function app does not have the MicrosoftTeams module installed. You need to include a reference to this module to the requirements.psd1 file (see https://learn.microsoft.com/azure/azure-functions/functions-reference-powershell#dependency-management for more details).
Currently this module is not yet natively integrated into the azure functions under powershell
To see all the available packages go in App Service -> Advanced Tools -> DebugConsole -> Powershell and run :
Write-Output ‘Getting PowerShell Module’
$result = Get-Module -ListAvailable |
Select-Object Name, Version, ModuleBase |
Sort-Object -Property Name |
Format-Table -wrap |
Out-String
Write-output `n$result
To manually add a package, It is necessary to create a directory "Module" At the same level as the directory of the function, They will be automatically preloaded.
(https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell step "Function app-level Modules folder")
After installation of the module. use below code in your script to automate the process.
$securedpassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredentials = New-Object System.Management.Automation.PSCredential ($Username, $securedpassword )
$res = Connect-MicrosoftTeams -Credential $mycredentials

How to install a software on a virtual machine using custom extension script

i would like to install the software(.msi file) on a virtual machine using custom extension script.
Below is my powershell custom extension script.
$computername = 'testingpurpose'
$sourcefile = "//fossies.org/windows/misc/mysql-workbench-community-8.0.18-winx64.msi"
$destinationFolder = "\\$computername\C$\Temp"
Copy-Item -Path $sourcefile -Destination $destinationFolder
Invoke-Command -ComputerName $computername -ScriptBlock { Msiexec /i //fossies.org/windows/misc/mysql-workbench-community-8.0.18-winx64.msi /log C:\MSIInstall.log }
(or)
Start-Process -destinationFolder //fossies.org/windows/misc/mysql-workbench-community-8.0.18-winx64.msi -ArgumentList '/i',$destinationFolder,'/q' -Wait -PassThru -Verb "RunAs"
According to my research, we can use choco to install mysql-workbench on windows Vm. For more details, please refer to the blog.
My script is as below
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco feature enable -n=allowGlobalConfirmation
choco install mysql.workbench

Display all bindings of site from IIS

Is there any powershell script or any vbscript which can display all bindings of sites from IIS?
Try the Get-WebBinding cmdlet (IIS 7 and up):
Import-Module WebAdministration
Get-WebBinding
Try this:
Import-Module WebAdministration
$Websites = Get-ChildItem IIS:\Sites
$site = $websites
$site.bindings.Collection

Resources