Certain commands like IISRESET and ROBOCOPY have stopped working in Powershell v4

亡梦爱人 提交于 2021-02-20 04:35:27

问题


Certain commands like IISRESET and ROBOCOPY have stopped working in Powershell v4

I have a pretty large script that runs those 2 commands at certain points, but these now both give me the general error:

iisreset : The term 'iisreset' is not recognized as the name of a cmdlet, function, 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.

These commands still work from CMD.

The only changes that were made is that I installed WMF5 and Powershell 5, but have yet to restart the server (a lot of copies to USBs running on them in the datacenter), so I'm still running Powershell 4. I even installed Powershell 7 and tried running the script but it mentioned that it was a PS4 module and that I needed WMF5 installed along with Powershell 5.1 (which is pending install. Well 5.0 is..)

This powershell script is something that is very crucial for me and I can't reboot for a couple of days.

Do you think the WMF5 install caused this or the Powershell 7 install? I removed Powershell 7 after I saw that the script was no longer working. Any workarounds until I can reboot, assuming that is the issue?

Windows Server 2012 R2 Powershell 4.0 with 5.0 pending reboot

I also just installed CMDer but I don't think it will have anything to do with it.


回答1:


iisreset is not really a powershell command, its an EXE file located in C:\Windows\System32 So first check that the file is there, if its there check your environment variable $Env:Path in PowerShell. That should contain C:\WINDOWS\system32; and many other paths.

Check $Env:Path by running this:

($env:path).split(";")

If you have the iisreset.exe file in C:\WINDOWS\system32 and C:\WINDOWS\system32 is added to the $Env:Path in PowerShell you should be able to run it by doing typing: iisreset.exe

To add the path to $Env:Path

$ENV:PATH="$ENV:PATH;C:\WINDOWS\system32"

But that wont be permanent unless you update the registry:

$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newpath = "$oldpath;C:\WINDOWS\system32"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

Then to validate its correct run:

($env:path).split(";")


来源:https://stackoverflow.com/questions/63144984/certain-commands-like-iisreset-and-robocopy-have-stopped-working-in-powershell-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!