提供凭据时启动进程会引发错误 - 可能存在错误

发布于 2024-12-03 07:28:50 字数 1562 浏览 2 评论 0原文

您可能知道为什么会在响应下面的代码时引发此错误吗?用户名和密码已被验证正确。

$secPassword = ConvertTo-SecureString "Password" -AsPlaintext -Force 
$farmCredential = New-Object System.Management.Automation.PsCredential "SharePoint\SP_Farm",$secPassword

Start-Process $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait

错误;

Start-Process : This command cannot be executed due to the error: The directory name is invalid.
At C:\Users\Administrator.SHAREPOINT\AppData\Local\Temp\fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1:8 char:14
+ Start-Process <<<<  $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output 
`"Hello:`"`$outvar1`"}`"" -Wait
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

但是,这工作得很好。

Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait

注意:这是在 PowerGUI 或 ISE ide 中执行时的情况 文件 fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1 确实存在于路径位置,因此由于某种原因 ide 遇到了困难。然而,当直接在 power shell 命令提示符/shell 中运行时,它确实可以工作。 我使用以本地管理员身份运行的本地计算机帐户登录,该脚本将执行定向到没有管理员权限的域帐户,并且仅以用户权限运行。

这是一个错误吗?因为作为开发人员,IDE 不应该因此而被绊倒,因为当我在 powershell 命令提示符窗口中运行该块时它可以工作?

Would you possibly know why this error is being raised in response to the code below. User-name and password have been verified as correct.

$secPassword = ConvertTo-SecureString "Password" -AsPlaintext -Force 
$farmCredential = New-Object System.Management.Automation.PsCredential "SharePoint\SP_Farm",$secPassword

Start-Process $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait

the error;

Start-Process : This command cannot be executed due to the error: The directory name is invalid.
At C:\Users\Administrator.SHAREPOINT\AppData\Local\Temp\fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1:8 char:14
+ Start-Process <<<<  $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output 
`"Hello:`"`$outvar1`"}`"" -Wait
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

however, this works just fine.

Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait

NOTE: this is when executing from within PowerGUI or the ISE ide's
The file fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1 does exist at the path location, so for some reason the ide is having dificulty with this. Yet it DOES work when ran directly within the power shell command prompt/shell.
I was logged in with a local machine account that is running as local admin, the script directs execution to a domain account which does not have admin rights and would run with just user permissions.

Is this a bug, since as a developer the IDE should not be tripped up by this as it works when i run the block in the powershell command prompt window??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

醉殇 2024-12-10 07:28:50

我有同样的错误。

此功能适用于 Powershell ISE,但不适用于 PowerGUI

Start-Process -FilePath "C:\WINDOWS\System32\cmd.exe" -Credential $credential -ArgumentList ("/c $sFileExecutable")

它适用于 WorkingDirectory 参数

Start-Process -FilePath 'cmd.exe' -Credential $credential -ArgumentList ("/c $sFileExecutable") -WorkingDirectory 'C:\Windows\System32'

I have the same bug.

This function is OK with Powershell ISE, but doesn't work with PowerGUI

Start-Process -FilePath "C:\WINDOWS\System32\cmd.exe" -Credential $credential -ArgumentList ("/c $sFileExecutable")

It works with the WorkingDirectory parameter

Start-Process -FilePath 'cmd.exe' -Credential $credential -ArgumentList ("/c $sFileExecutable") -WorkingDirectory 'C:\Windows\System32'
残疾 2024-12-10 07:28:50

对问题的最好解释隐藏在评论中Nathan Hartley,让我在这里总结一下:

该问题仅与文件系统权限相关,与主机环境(控制台与 ISE)无关:

  • 当您使用 Start-Process 而不使用 -WorkingDirectory 指定目标目录时,PowerShell 的当前位置(目录)也会用于目标进程。

  • 由于您使用-Credential作为其他用户运行 - 此时没有提升 - 目标用户可能缺乏访问当前用户的权限目录,例如,如果当前目录位于当前用户的主目录子树内,则会发生这种情况。

    • 遗憾的是,PowerShell 的错误消息通过误导性报告掩盖了此原因:目录名称无效。

修复

  • 要么确保当前位置可访问到目标用户,
  • 或者最好使用-WorkingDirectory参数显式设置目标进程的当前目录。

例如,从所在目录启动目标进程目标脚本是找到,你可以使用类似的东西:

$script = 'c:\path\to\your\script.ps1'
Start-Process -WorkingDirectory (Split-Path $script) -Credential ...

The best explanation of the problem is buried in a comment by Nathan Hartley, so let me summarize it here:

The issue is solely related to filesystem permissions, and has nothing to do with the host environment (console vs. ISE):

  • When you use Start-Process without specifying a target directory with -WorkingDirectory, PowerShell's current location (directory) is used for the target process as well.

  • Since you're using -Credential to run as a different user - without elevation at that point - the target user may lack permission to access the current directory, which happens if the current directory is inside the current user's home directory subtree, for instance.

    • Unfortunately, PowerShell's error message obscures this cause by misleadingly reporting: The directory name is invalid.

Fix:

  • Either make sure that the current location is accessible to the target user,
  • or, preferably, use the -WorkingDirectory parameter to explicitly set the target process's current directory.

For instance, to start the target process from the directory in which a target script is located, you could use something like:

$script = 'c:\path\to\your\script.ps1'
Start-Process -WorkingDirectory (Split-Path $script) -Credential ...
盛夏已如深秋| 2024-12-10 07:28:50

这是一个奇怪的错误,但我重新创建了该错误并修复了它...

http://support.microsoft. com/kb/832434

基本上,将 Powershell_ISE(或 PowerGUI!)的启动目录修改为系统范围的值。

This is a weird one but I recreated the error and this fixed it...

http://support.microsoft.com/kb/832434

Basically, modify the start-in directory for Powershell_ISE (or PowerGUI!) to a system-wide value.

旧街凉风 2024-12-10 07:28:50

我知道这已经很晚了,但是该帖子对我有帮助(特别是来自@Dionysoos的建议),并希望我的回答可以帮助别人。

我遇到了同样的错误...

Start-Process : This command cannot be executed due to the error: The directory name is invalid.

...在无人值守的情况下运行脚本时,而它在 ISE 中工作。

无人值守脚本使用特定于用户的 $env:TEMP 作为工作目录,这意味着新进程无权访问它。在 Start-Process 命令上指定 -WorkingDirectory $env:windir 解决了该问题。

I know this is rather late, but the thread helped me (particulary the suggestion from @Dionysoos), and hope my answer might help others.

I had the same error...

Start-Process : This command cannot be executed due to the error: The directory name is invalid.

...when running a script unattended, while it was working in the ISE.

The unattended script was using the user-specific $env:TEMP as the working directory which meant that the new process did not have access to it. Specifying -WorkingDirectory $env:windir on the Start-Process command resolved the issue.

卷耳 2024-12-10 07:28:50

我知道这可能有点晚了,但是当当前目录是网络路径时您是否运行该命令?我遇到过这个问题,如果我从系统驱动器运行相同的命令,它就可以工作。

I know this might be a bit late, but are you running that command when the current directory is a network path? I have experienced this as a problem and if I run the same command from a system drive it works.

秋日私语 2024-12-10 07:28:50

就我而言,原因是 Windows 10 保存了以与我预期不同的方向创建新文件,我尝试将 profile.ps1 文件添加到 C:\Users\Username\ Documents\WindowsPowerShell 但它应该是 C:\Users\Username\**OneDrive**\Documents\WindowsPowerShell
所以请尝试按照以下步骤操作

RUN:
Test-Path C:\Users\User\Documents\WindowsPowerShell\
IF FALSE
New-Item -Path C:\Users\User\Documents\WindowsPowerShell\ -ItemType Directory
THEN
New-Item -Path $profile.CurrentUserAllHosts -Type File
THEN
start $profile.CurrentUserAllHosts

In my case, the reason was that Windows ten saved to create a new file in a different direction which I expected, I try to add profile.ps1 file to C:\Users\Username\Documents\WindowsPowerShell but it should be C:\Users\Username\**OneDrive**\Documents\WindowsPowerShell
so just try to follow these steps

RUN:
Test-Path C:\Users\User\Documents\WindowsPowerShell\
IF FALSE
New-Item -Path C:\Users\User\Documents\WindowsPowerShell\ -ItemType Directory
THEN
New-Item -Path $profile.CurrentUserAllHosts -Type File
THEN
start $profile.CurrentUserAllHosts
过度放纵 2024-12-10 07:28:50

将 -WorkingDirectory 设置为 exe 目录时仍然存在问题...
发现将 -WorkingDirectory 设置为 C:\Windows\System32 并使用 fq exe 路径有效。

Still had issue with setting -WorkingDirectory to exe directory...
found that setting -WorkingDirectory to C:\Windows\System32 and used fq path to exe worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文