I have an old script that invokes itself with some extra params in a certain situation. This works if run through a regular powershell window
$spath = $script:MyInvocation.MyCommand.Path
$FilePathWithQuotes = '"{0}"' -f $spath
powershell -file $FilePathWithQuotes -NestedCall #ExtraArgs
but if it's run through the ISE I get this error.
powershell : Add-Type : Cannot bind parameter 'Path' to the target.
Exception setting "Path": "Cannot find path At D:\Deploy\File
Deploy.ps1:39 char:5
+ powershell -file $FilePathWithQuotes -NestedCall #ExtraArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Add-Type : Cann...nnot find path :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
I added the second line to make sure the spaces weren't the issue but it still fails in the ISE.
Thoughts? Im on ps 5.1
Yeah, check for $PSISE, and use that if possible.
$spath = If($PSISE){$psISE.CurrentFile.FullPath}else{$script:MyInvocation.MyCommand.Path}
$FilePathWithQuotes = '"{0}"' -f $spath
powershell -file $FilePathWithQuotes -NestedCall #ExtraArgs
Related
I have a C++ application that launches a ps1 script using the following command
ShellExecuteA(0, "open", "powershell.exe", -executionpolicy RemoteSigned -File script.ps1", NULL, SW_NORMAL)
The script contains the following
$version = [System.Environment]::OSVersion.Version.Major
if ($version -gt 9)
{
$app = 'C:\Windows\System32\uwfmgr.exe'
$appargs = 'servicing enable'
Start-Process $app $appargs
Start-Sleep -s 5
$appargs= 'filter restart'
Start-Process $app $appargs
}
The end result should be that the Windows 10 machine (protected by UWF) should request service mode on its next reboot and the computer should restart
What happens instead is that the following error is raised
Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At script.ps1:6 char: 2
+ Start-Process $app $args
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Edit:
Added
$Error[0] | FL * -Force
and renamed $args to $appargs
Total newbie here...
I am attempting to execute a ps1 file using Powershell and receiving an error. What does this error mean? I'm concerned that OneDrive is causing an issue with the path.
Running this ps1 file by opening powershell and entering:
PowerShell -executionpolicy Bypass -NoExit C:\Users\Terry\OneDrive\desktop\WindowsPull.ps1
This is the file:
$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}
Error:
Set-Location : Cannot process argument because the value of argument "path" is null. Change the value of argument
"path" to a non-null value.
At C:\Users\Terry\OneDrive\desktop\WindowsPull.ps1:4 char:1
+ Set-Location $videoDir.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], PSArgumentNullException
+ FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand
I found a way to get the current working directory into $dp0.
PS C:\src\powershell> Get-Content .\curdir2.ps1
$dp0 = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Set-Location -Path $dp0
Write-Host "location is set"
Set-Location -Path [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Write-Host (Get-Location).Path
Why is it that when I try to use the same way as a parameter to Set-Location it is an error? I think this may be something fundamental about the objects in Powershell. What do I need to know?
Set-Location : A positional parameter cannot be found that accepts argument 'C:\src\powershell\curdir2.ps1'.
At C:\src\powershell\curdir2.ps1:6 char:1
+ Set-Location -Path [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.De ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
Use parentheses.
As you can see in PS ISE that entire parameter is interpreted as a literal string otherwise.
And always use -LiteralPath instead of -Path to correctly handle directories with [] brackets.
Set-Location -LiteralPath ([IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition))
PS3.0+: cd -LiteralPath $PSScriptRoot
What am I doing wrong here? I am trying to open a second PS1 file if True but an error is thrown (shown below).
#ABOUT
#GLOBALS
$userID = "admin"
$pswrd = "test"
$name = Read-Host 'What is your username?'
$pass = Read-Host 'And your password?' #-AsSecureString | ConvertFrom-SecureString
#$script = '.\sdsSysMain.ps1'
if($name -eq $userID -and $pass -eq $pswrd) #or blank?
{
#write-host "Well done! You're in. "
#Start-Sleep -s 5
Powershell -noexit ".\sdsSysMain.ps1"
}
elseif($name -ne $userID -or $pass -ne $pswrd)
{
write-host "Login Failed... :("
}
This is the error I'm getting:
powershell.exe : The term '.\sdsSysMain.ps1' is not recognized as the name of a cmdlet, function
At C:\Users\1234\Documents\Projects\sdsSys\sdsSysLogin.ps1:19 char:15
+ Powershell <<<< -noexit ".\sdsSysMain.ps1"
+ CategoryInfo : NotSpecified: (The term '.\sds...mdlet, function:String) [],
RemoteException
+ FullyQualifiedErrorId : NativeCommandError
, 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.
At line:1 char:17
+ .\sdsSysMain.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (.\sdsSysMain.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The error is rather self-explanatory. The script doesn't find sdsSysMain.ps1 in the current working directory. You can output the current working directory in your script like this:
(Get-Location).Path
If you want to run sdsSysMain.ps1 from the same directory as the script calling it, change this:
Powershell -noexit ".\sdsSysMain.ps1"
into this:
$scriptPath = Split-Path -Parent $script:MyInvocation.MyCommand.Path
Powershell -NoExit -File "$scriptPath\sdsSysMain.ps1"
I am trying to write a PowerShell script to perform steps on multiple Word docs. I have Word 2010 installed on my machine, but I can't seem to get the script to open the docs. Here is the script
$path = "C:\MyPath"
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
$objWord = New-Object -ComObject "word.application"
$objWord.visible = $false
foreach($wd in $wordFiles)
{
$doc = $objWord.documents.open($wd.fullname)
#InsertProcessingFunctionsHere
$doc.Save()
$objWord.Documents.Close()
}
$objWord.Quit()
I try and run this, and the error I get back from PowerShell is:
Exception calling "Open" with "1" argument(s): "Command failed"
At C:\Scripts\Process-WordDocs.ps1:10 char:31
+ $doc = $objWord.documents.open <<<< ($wd.fullname)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\Process-WordDocs.ps1:13 char:10
+ $doc.Save <<<< ()
+ CategoryInfo : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Close" with "0" argument(s): "This method or property is not available because a document window is not active."
At C:\Scripts\Process-WordDocs.ps1:14 char:25
+ $objWord.Documents.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
MSDN states that documents.open only requires 1 argument, and the rest are optional. However, a C# example I have seen on the net, showed passing a "ReadOnly: False" parameter to documents.open. Stepping through the script in the ISE Debugger, I can see $wd.fullname is there and points to a valid file, so I am completely unclear why it is not opening. At first, I thought this was because I was using a 64-bit version of the OS (32-bit version of Office), but attempting the script from a 32-bit PowerShell Session resulted in the same error. Anyone have any insight here as to why this may be happening, and how I can fix it? I would prefer all the processing to happen invisible to the user. Any help would be greatly appreciated. Thank you in advance for your time.
I think you want to close the document using $doc.close() instead of $objWord.Documents.Close()