I am trying to call a file enable.ps1 from another file begin.ps1. Both the files are in the same folder. So, I thought that may be I can use the following code for that purpose.
Here is the code I have written inside begin.ps1 for calling.
#
# begin.ps1
#
function MapDrives ($Message)
{
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPath\enable.ps1"
cmd /c pause | out-null
Start-Sleep -s 20
}
There is the PowerShell file I am trying to call: enable.ps1
I am using Visual studio 2015
Windows 7
PowerShell 5
both begin.ps1 and enable.ps1 are under same folder location which is this :
C:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test
Do you have any idea on how should I proceed on this ?
P.S : did some changes as suggested by Martin. Now the code looks like this:
function MapDrives ($Message)
{
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath\enable.ps1"
cmd /c pause | out-null
Start-Sleep -s 20
}
And, I am trying to run it in PowerShell ISE. It giving this error.
Network drives does not exist till now. Trying again to connect
...............................................................
& : The module 'param($Message)
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath' could not be loaded. For more information, run 'Import-Module param($Message)
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath'.
At C:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test\begin.ps1:45 char:7
+ & "$ScriptPath\enable.ps1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (param($Message)...cmd \enable.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule
You are doing it right. However it looks like $ScriptPath doesn't contain any value (as you see within the error message).
You probably have to replace
$ScriptPath = Split-Path $MyInvocation.InvocationName
with
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Related
I have the following Powershell script:
# Check if the script is running as admin
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $IsAdmin) {
# Display an error message and exit
Write-Output "Dieses Programm muss als Administrator ausgeführt werden. Bitte Rechtsklicke diese datei und wähle 'Als Administrator' ausführen."
Start-Sleep -Seconds 50
break
}
$PortName = Read-Host -Prompt "Gib deine Druckeradresse ein"
Invoke-WebRequest -Uri "https://dl.konicaminolta.eu/de?tx_kmdownloadcentersite_downloadproxy%5bfileId%5d=7f86746b8a2082a5152d3dd65a48292d&tx_kmdownloadcentersite_downloadproxy%5bdocumentId%5d=135985&tx_kmdownloadcentersite_downloadproxy%5bsystem%5d=KonicaMinolta&tx_kmdownloadcentersite_downloadproxy%5blanguage%5d=DE&type=1558521685" -OutFile "C:\printer.zip"
Expand-Archive -Path "C:\printer.zip" -DestinationPath "C:\printerAksa"
pnputil.exe /add-driver "C:\printer\UPD4PCL6Win81P_2101MU\driver\x64\PCL6\KOBxxK__01.inf" /install
Add-PrinterDriver -Name “KONICA MINOLTA Universal V4 PCL”
Add-Printer -PortName $PortName -DriverName "KONICA MINOLTA Universal V4 PCL" -Name "Printer"
Remove-Item -Path "C:\printer.zip" -Force
Remove-Item -Path "C:\printer" -Recurse -Force`
It is meant to install the printers for our school because at the moment the school's solution is to install is manually, but many people aren't tech savvy enough to do that, even with a step-by-step guide.
So i created this script in which one only has to enter their own printer address provided by the school and the rest is done for them. The problem now is that you cant really just share PowerShell scripts, because you'd have to change a policy to execute it (at least to my knowledge). What I tried is converting it to an exe file using PS2EXE which worked but when others downloaded it, Windows blocked the download as it was identified as a virus.
So here is the Question: How do I bring my little program to others from the school without it getting flagged? Maybe there is even a completely different approach than mine to the problem altogether.
Thanks for your suggestions!
I wrote a ps1 script to automate some package installation but the strange part is when I run the command snippet for executing the .exe file for SEP (Symantec Endpoint Protection) , it is executing fine , but when I execute the entire script , it does run the command snippet.
Iam only running a simple .exe file , and even if I run it manually , it does not show any installer , rather it installs silently in the background.
So in the script, Iam only running the .exe file, thats it .
Should I be giving any wait time or any other inputs ?
Start-Process -Wait -FilePath "C:\Temp\Symantec-Windows\SEP 14.3.3384.1000 x64.exe" -passthru
$SymVersion = Get-WmiObject -Class Win32_Product -ComputerName $hostname | Where-Object -FilterScript {$_.Name -eq "symantec endpoint protection"} | Format-List -Property version, InstallState, name
echo $SymVersion
if($SymVersion)
{
echo 'Symantec is successfully installed' -ForegroundColor Green
}
else
{
echo 'Symantec is not successfully installed' -ForegroundColor Red
}
The symantec antivirus exe files are made for silent installations. If you want to proceed with GUI mode, better unzip the file and use MSI file with arguments. With your current script,Its better to check the process is exited with code 0. The following code is not tested.
$process = Start-Process -FilePath "C:\Temp\Symantec-Windows\SEP 14.3.3384.1000 x64.exe" -passthru -Wait
if($process.ExitCode -ne 0)
{
throw "Installation process returned error code: $($process.ExitCode)"
} else { Write-Host "Installation Successful"}
I'm trying to get a script together to remotely install some windows updates on some remote servers that are connected in an offline domain.
I have tried regular PS Remoting and after some research, I think what I am trying to do isnt supported by microsoft. When checking my event logs I have a bunch of these errors.
Edit
I wanted to add that I have tried running the .\Install2012R2.ps1 script from my local computer, modified to have the Invoke-Command in that and have it run the update portion of the original Install2012R2.ps1 and I would get the same errors.
I was hoping that by placing the script on each server that it would like that more.
End Edit
Windows update could not be installed because of error 2147942405 "Access is denied."
(Command line: ""C:\Windows\System32\wusa.exe" "C:\Updates\windows8.1-kb4556853-x64.msu" /quiet /norestart")
I have tried running Invoke-Command as credentialed to an administrator account on the servers but I have been having no luck and was looking for some advice if someone has maybe tried/done this before.
$Servers = #("V101-Test1","V101-Test2")
$Username = 'admin'
$Password = 'Password'#not actual password
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Get-PSSession | Remove-PSSession
New-PSSession -ComputerName $Servers
foreach($Server in $Servers){
Get-ChildItem -Path C:\Source\Temp -Recurse | Copy-Item -Destination "\\$Server\c$\Updates\" -Force
}
Invoke-Command $Servers -Credential $Cred -ScriptBlock{
& "C:\Updates\Install2012R2.ps1"
}
EDIT 2
Here is the actual install code of the Install2012R2.ps1 script
$updatedir= "./"
$files = Get-ChildItem $updatedir -Recurse
$msus = $files | ? {$_.extension -eq ".msu"}
$exes = $files | ? {$_.extension -eq ".exe"}
foreach ($file in $msus){
$KBCtr++
$fullname = $file.fullname
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
$KBN = $fullname.split('-')[1]
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
# Specify the command line parameters for wusa.exe
$parameters = $fullname + " /quiet /norestart"
# Start services and pass in the parameters
$install = [System.Diagnostics.Process]::Start( "wusa",$parameters )
$install.WaitForExit()
}
I'm not sure why wusa.exe is failing here with Access Denied, but here is a PowerShell-native approach you can try. If nothing else, it should give you a clearer indication via the captured error information as to what the underlying issue is:
Add-WindowsPackage -Path C:\Updates\OurHeroicUpdate.msu -Online -PreventPending -NoRestart
-Path is the path to the msu file
-Online tells Add-WindowsPackage to modify the currently "mounted image" (the running version) of Windows (as opposed to an offline disk image you could also apply it to)
-PreventPending prevents installing the msu if there is already a pending change, like needing to reboot for updates.
Add-WindowsPackage is part of the DISM module available under Windows PowerShell, and is the functional equivalent of dism /packagepath:"cabfile", although it can take an msu where dism.exe only allows a cab.
I have an update script for running the Dell Command Update tool. In short dcu-cli.exe. The thing now is than when i run the same script code on the computer local then everything runs OK but when i run the exact same code in a script with invoke-command(and yes i have full admin rights) than the exitcode is 2 meaning An unknown application error has occurred instead of 0 (everything OK)
It is a very large script so i created a new one to debug this. This is the shorted code:
Invoke-Command -ComputerName "MyComputer" -ScriptBlock {
$ExitCode = 0
#Declare path and arguments
$DcuCliPath = 'C:\Program Files (x86)\Dell\CommandUpdate\dcu-cli.exe'
$DellCommand = "/applyUpdates -autoSuspendBitLocker=enable -outputLog=C:\Dell_Update.log"
#Verify Dell Command | Update exists
If (Test-Path -Path $DcuCliPath) {
$objWMI = Get-WmiObject Win32_ComputerSystem
Write-Host ("Dell Model [{0}]" -f $objWMI.Model.Trim())
$serviceName = "DellClientManagementService"
Write-Host ("Service [{0}] is currently [{1}]" -f $serviceName, (Get-Service $serviceName).Status)
If ((Get-Service $serviceName).Status -eq 'Stopped') {
Start-Service $serviceName
Write-Host "Service [$serviceName] started"
}
#Update the system with the latest drivers
Write-Host "Starting Dell Command | Update tool with arguments [$DellCommand] dcu-cli found at [$DcuCliPath]"
$ExitCode = (Start-Process -FilePath ($DcuCliPath) -ArgumentList ($DellCommand) -PassThru -Wait).ExitCode
Write-Host ("Dell Command | Update tool finished with ExitCode: [$ExitCode] current Win32 ExitCode: [$LastExitCode] Check log for more information: C:\Dell_Update.log")
}
}
When i remove the Invoke-Command -ComputerName "MyComputer" -ScriptBlock { and then copy + run the script local on the PC then the exitcode = 0
What i also noticed than when i run the command via 'Invoke-Command' then there is also no log file created as i passed along in the arguments... So my best guess is something is going wrong with local an remote paths?
So what am i missing? I'm guessing it is something simple but i spend several hours to get this running without any luck...
Try running it this way. You should be able to see any output or error messages. I typically add to the path first rather than using & or start-process.
invoke-command mycomputer {
$env:path += ';C:\Program Files (x86)\Dell\CommandUpdate';
dcu-cli /applyUpdates -autoSuspendBitLocker=enable -outputLog=C:\Dell_Update.log }
Using start-process inside invoke-command seems pretty challenging. I can't even see the output of findstr unless I save it to a file. And if I didn't wait the output would be truncated. By default start-process runs in the background and in another window. There's a -nonewwindow option too but it doesn't help with invoke-command.
invoke-command localhost { # elevated
start-process 'findstr' '/i word c:\users\joe\file1' -wait -RedirectStandardOutput c:\users\joe\out }
#js2010, thanks for your additional help. Unfortunately this didn't helped either.
So i did some more debugging and it turns out it was a bug in the dcu-cli version running on my test machine, DOH...!!
On the test machine version 3.1.1 was running and on another machine version 4.0 was running and that worked fine via remote Powershell. So i looked for the release notes, which i found here: https://www.dell.com/support/kbdoc/000177325/dell-command-update
And as you can see in version 3.1.3 there was this fix:
A problem was solved where dcu-cli.exe was not executed in an external interactive session of PowerShell.
I'm trying to run the following code in a function in my script:
$result = Mount-DiskImage -ImagePath $imagepath -PassThru
$driveLetter = ($result | Get-Volume).DriveLetter
Set-Location "$($driveLetter):"
But it constantly fails with this error:
Set-Location : Cannot find drive. A drive with the name 'G' does not exist.
At C:\Users\Agent\BuildAgent\scripts\helpers.psm1:35 char:3
+ Set-Location "$($driveLetter):"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (G:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
But after the script has terminated I can change the drive, no problem.
It might be timing related, but injecting a sleep (even a large one) before setting location, does not help.
Does anyone know about this issue?
Let's all up-vote the bug report.
Then, I think I have a workaround by manually adding the drive as a new PSDrive. I guess the New-PSDrive cmdlet can access the mounted drive even though others can't.
$mount = Mount-DiskImage $isoPath -PassThru
$driveLetter = ($mount | Get-Volume).DriveLetter
# Have to use New-PSDrive so other cmdlets in this session can see the new drive
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root "$($driveLetter):\"
// ...do things...
Dismount-DiskImage $mount.ImagePath
I can't try this but your command looks odd to me
Set-Location "$($driveLetter):"
have you tried
Set-Location "$driveLetter:"
without the parens