Hi I currently have a very basic PowerShell script that runs when a computer is installed with Windows (any version). This script is below
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Write-Output "Installing OS Updater Modules Please Wait...."
Write-Output " "
Install-PackageProvider -Name NuGet -Force
Install-Module -Name PSWindowsUpdate -Force
Write-Output " "
Write-Output "Installing Updates...."
Write-Output " "
Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot | Out-File "c:\temp\WindowsUpdate.log" -force
As I said very simple code this will install a few modules then update windows with any updates, however, it only works with PowerShell 5 and above is there a way I can check what version is installed then carry on with the script if it is PowerShell 4 and below stop executing the script.
I know it's possible to install Windows Management Framework 5.1 into the install.wim/esd which will get around this but for people who don't have this, I would like the script to stop and not have any errors.
u can use cmdlet named "get-host", it works in ps version >=2.0
if ((get-host).version.major -le 4)
{
#code
}
else
{
#code
}
or if u need exact match , u can use switch construction
switch ((get-host).version.major)
{
4 {#installcode4}
5 {#installcode5}
7 {#installcode7}
}
Related
So I am trying to write a script that will check if the user has a certain module installed, and if it doesn't, to install it, then rerun itself.
When I try to run this, the script just keeps rerunning and trying to install. I have to use a setup.exe and I have it waiting for the window to cl
$mypath = $MyInvocation.MyCommand.Path
$Path = Split-Path $mypath -Parent
$Location = "$Path" + "\setup.exe"
if (Get-Module -ListAvailable -Name ActiveRolesManagementShell) {
Write-Host "QAD Is installed"
pause
}
else{
Write-Host "Installing QAD"
Start-Process $Location
Wait-Process -Name "setup"
Pause
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Pause
}
Normally, modules are installed using Install-Module. Then you might need to tell PowerShell about it for the current session to be able to use it.
if (-not(Get-Module -ListAvailable -Name ActiveRolesManagementShell)) {
Install-Module ActiveRolesManagementShell
Import-Module ActiveRolesManagementShell # Might not be needed
}
# Add code that uses ActiveRolesManagementShell module
But with regards to ActiveRolesManagementShell, what version are you using?
The old Quest module was probably meant to work on PowerShell v2. And I can't find a current version...
I have written a simple script to massively download and install Windows Updates onto fresh OS installations. For most of them the script well accomplish the installation. Last but not the least, the most recent cumulative update is downloaded and installation started. But the installation never ends (ie the progress bar does not complete) and the script remains hanged up. How to force the finalization?
Edit: the same task, from the Windows Update GUI, works
Here there is my script:
# elevated script execution with admin privileges
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}
# setting script execution policy
$ErrorActionPreference= 'SilentlyContinue'
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force
$ErrorActionPreference= 'Inquire'
$ErrorActionPreference= 'Stop'
try {
Import-Module PSWindowsUpdate
} catch {
Install-Module PSWindowsUpdate -Confirm:$False -Force
Import-Module PSWindowsUpdate
}
$ErrorActionPreference= 'Inquire'
# list of available updates
# Get-Windowsupdate
# install the updates
Install-WindowsUpdate -AcceptAll -Install -Confirm:$False
I'm trying to make a silent install of an AMD driver with Powershell, but for some reason, I always get the AMD installation screen.
My arguments seem to be ok because I do not have to click anywhere and the installation completes by itself.Is there any way to install it without any windows popping up? I can install 7zip silently the same way without any problem.
Set-ExecutionPolicy Unrestricted
$Logpath = 'C:\powershell.log'
function Install_app
{
$exe_to_execute = 'C:\Setup.exe'
$argument = '/unattended_install:"..\Packages\Drivers\Display\W76A_INF;..\Packages\Drivers\amdkmpfd\W764a;..\Packages\Apps\ACP64;..\Packages\Apps\AppEx;..\Packages\Apps\CCC2;..\Packages\Apps\CIM;..\Packages\Apps\VC12RTx64\" /autoaccept_all /force_hide_first_run /force_close_when_done /on_reboot_message:no'
$process = Start-Process -FilePath $exe_to_execute -ArgumentList $argument -Wait -PassThru -NoNewWindow
# Loop until process exits
do {start-sleep -Milliseconds 500}
until ($process.HasExited)
# Log results
$(Get-Date).ToString() + " Exit code " + $process.ExitCode | Out-File $Logpath -Append
}
Install_app
My script is in fact actually working as it is. The problem is that it will only hide all setup windows if run under the system account.
I had written a PowerShell script to download and install windows updates as follows. This script will run powershell to run as administrator. I run on a windows 7 computer but it keep having a loop of opening the powershell windows. pls advise
enter code here
If (-NOT ([Security.Principal.WindowsPrincipal]
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole
([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
#define type of updates
$criteria="Isinstalled=0 and Type='driver'"
#Search for relevant updates
$Searcher=New-Object -ComObject Microsoft.Update.Searcher
$SearchResult=$Searcher.Search($criteria).updates
if($SearchResult.Count -gt 0) {
$Session=New-Object -ComObject Microsoft.Update.Session
$Downloader=$Session.CreateUpdateDownloader()
$Downloader.Updates=$SearchResult
$Downloader.Download()
$installer=New-Object -ComObject Microsoft.Update.Installer
$installer.Updates=$SearchResult
$Install_Begin=$installer.Install()
}
break
}
to be honest, if you are trying to install updates from PowerShell you can do this from two easy lines of code.
The first would be to install the module for updates and the second would be to run the updates.
Please see the code below for updating windows.
install-module pswindowsupdate
Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
I am trying to sort out an error on my boss' computer when he runs a PowerShell command (to build our software). He is the first developer running Windows 10. All others are still on Win 8.1.
The error looks like Write-Verbose is not working on Windows 10. But I find that hard to believe:
powershell.exe -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden
-ExecutionPolicy RemoteSigned
-Command "Import-Module 'X:\ReleaseRoot\CompanyName\Deployment\CompanyName.Build'
-DisableNameChecking -Force; Generate-SvDeployManifest " -verbose
Write-Verbose : The Win32 internal error "No process is on the other end of
the pipe" 0xE9 occurred while getting console output buffer information.
Contact Microsoft Customer Support Services.
At X:\ReleaseRoot\CompanyName\Deployment\CompanyName.Build\Functions\Nuget\Restore-NugetPackages.ps1:23 char:9
+ Write-Verbose "Restoring packages from $_"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ Category Info : ResourceUnavailable: (:) [Write-Verbose], HostException
+ FullyQualifiedErrorId : GetConsoleScreenBufferInfo,Microsoft.PowerShell.Commands.WriteVerboseCommand
The PowerShell function that is failing is very simple:
function Restore-NugetPackages
{
$packageConfigs = #("$RootFolder\Build\nuget\packages.config")
$packageConfigs | ForEach {
Write-Verbose "Restoring packages from $_"
& $RootFolder\Build\nuget\nuget.exe restore $_ -PackagesDirectory "$RootFolder\Build\packages" | Write-Verbose
# Output the versions of the nuget packages
([xml] ( Get-Content $_ )).packages.package | Select id, version
}
}
This function works perfectly on Windows 8.1.
I'm stumped. Any clues?
Is he using the 32bit or 64bit version of powershell. There are some inconsistencies with the 32 bit. It seems like the verbose stream is not there.