powershell“;用户32“;使用dllImport时,当前上下文中不存在
我正在尝试创建一个快捷方式来启动powershell命令隐藏 我正在使用在堆栈溢出上找到的代码:powershell“;用户32“;使用dllImport时,当前上下文中不存在,powershell,Powershell,我正在尝试创建一个快捷方式来启动powershell命令隐藏 我正在使用在堆栈溢出上找到的代码: $t='[DllImport(“user32.dll”)]公共静态外部boolshowwindow(int句柄,int状态);';添加type-name-win-member$t-namespace-native;[native.win]::ShowWindow([System.Diagnostics.Process]::GetCurrentProcess()| Get Process).MainW
$t='[DllImport(“user32.dll”)]公共静态外部boolshowwindow(int句柄,int状态);';添加type-name-win-member$t-namespace-native;[native.win]::ShowWindow([System.Diagnostics.Process]::GetCurrentProcess()| Get Process).MainWindowHandle,0)代码>
当我从powershell命令行执行它时,一切正常。但当我尝试从快捷方式执行它时,目标是:%windir%\system32\WindowsPowerShell\v1.0\powershell.exe
我犯了个错误
add-type : c:\Users\xxx\AppData\Local\Temp\ruaml1si.0.cs(8) : Name 'user32' does not exist in current context
c:\Users\xxx\AppData\Local\Temp\ruaml1si.0.cs(7) : {
c:\Users\xxx\AppData\Local\Temp\ruaml1si.0.cs(8): >>> [DllImport(user32.dll)] public static extern bool
ShowWindow(int handle, int state);
c:\Users\xxx\AppData\Local\Temp\ruaml1si.0.cs(9) :
如果有人能帮助我,我将非常高兴。谢谢。导入dll时需要添加入口点:
$t = '[DllImport("user32.dll",EntryPoint="ShowWindow")] public static extern bool ShowWindow(int handle, int state);';add-type -name win -member $t -namespace native;[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0);
导入dll时需要添加入口点:
$t = '[DllImport("user32.dll",EntryPoint="ShowWindow")] public static extern bool ShowWindow(int handle, int state);';add-type -name win -member $t -namespace native;[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0);
题外话:据我所知,快捷方式的目标长度似乎有260个字符的限制(如果通过右键单击下拉菜单中的“新建”»快捷方式生成)。显然,您的字符串超出了此限制
但是,如果您应用适当的规则来转义内部(嵌套)双引号,则问题可能会得到解决。例如:
echo“here$PWD”
如果从打开的powershell窗口执行,则可以工作,但其等效项是:
powershell.exe-noexit-command“echo”“here$PWD”“”
作为windows快捷方式的目标字符串(请注意,-noexit
仅用于查看输出),以及
powershell.exe-命令“echo”“here$PWD”“”
作为从打开的cmd
窗口运行的命令
- 等等
主题外注意:据我所知,快捷方式的目标长度似乎有260个字符的限制(如果通过右键单击下拉菜单中的“新建”快捷方式创建)。显然,您的字符串超出了此限制
但是,如果您应用适当的规则来转义内部(嵌套)双引号,则问题可能会得到解决。例如:
echo“here$PWD”
如果从打开的powershell窗口执行,则可以工作,但其等效项是:
powershell.exe-noexit-command“echo”“here$PWD”“”
作为windows快捷方式的目标字符串(请注意,-noexit
仅用于查看输出),以及
powershell.exe-命令“echo”“here$PWD”“”
作为从打开的cmd
窗口运行的命令
- 等等
如果我们从快捷方式谈论powershell的隐藏启动,那么这不是问题
C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -ExecutionPolicy Bypass & 'C:\You\Best\Program\Path.ps1' """"Par1"""" """"Par2"""""",0,false:close")
但是user32.dll库也需要用于其他目的,并且在从Bat文件调用powershell时,我也无法运行它
增加。问题已经解决。:)
当我从批处理文件中使用“usebackq”调用“user32.dll”时发生了这种情况。在查看powershell中未转义的代码后,发现user32.dll名称周围没有双引号。批处理文件中引用的初步复制-解决了问题。
在文件快捷方式中检查引号总是很棘手的。你应该再次检查他们是否直接出现在这里
增加。以下是你问题的答案:
%windir%\System32\WindowsPowerShell\v1.0\powershell -noexit -c add-type -m '[DllImport(""""user32.dll"""")] public static extern bool ShowWindow(int handle,int state);' -namespace n -name w;[n.w]::ShowWindow((Get-Process -Id $PID).MainWindowHandle,0);Echo OK
但是这个Windows电话线实际上没有任何意义。在自动关闭的Powershell调用行中,260个字符中仍有1个可用字符显然不足以执行任何任务:)
尽管如此,这种解决方案的意义可能只是当我们在批处理文件中编写Powershell代码,然后从批处理中启动它时才有位置。在本例中,我们没有260个字符的硬限制。这将避免注释出服务符号这一不必要的、有时无法解决的任务。
显然,使用Batch+js/vbs的Powershell hidden open解决方案在快捷方式中效率更高。如果我们谈论从快捷方式隐藏启动Powershell,那么这不是问题
C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -ExecutionPolicy Bypass & 'C:\You\Best\Program\Path.ps1' """"Par1"""" """"Par2"""""",0,false:close")
但是user32.dll库也需要用于其他目的,并且在从Bat文件调用powershell时,我也无法运行它
增加。问题已经解决。:)
当我从批处理文件中使用“usebackq”调用“user32.dll”时发生了这种情况。在查看powershell中未转义的代码后,发现user32.dll名称周围没有双引号。批处理文件中引用的初步复制-解决了问题。
在文件快捷方式中检查引号总是很棘手的。你应该再次检查他们是否直接出现在这里
增加。以下是你问题的答案:
%windir%\System32\WindowsPowerShell\v1.0\powershell -noexit -c add-type -m '[DllImport(""""user32.dll"""")] public static extern bool ShowWindow(int handle,int state);' -namespace n -name w;[n.w]::ShowWindow((Get-Process -Id $PID).MainWindowHandle,0);Echo OK
但是这个Windows电话线实际上没有任何意义。在自动关闭的Powershell调用行中,260个字符中仍有1个可用字符显然不足以执行任何任务:)
尽管如此,这种解决方案的意义可能只是当我们在批处理文件中编写Powershell代码,然后从批处理中启动它时才有位置。在本例中,我们没有260个字符的硬限制。这将避免注释出服务符号这一不必要的、有时无法解决的任务。
显然,使用Batch+js/vbs的Powershell隐藏打开解决方案在快捷方式方面效率更高。问题仍然存在。例如,此命令给出错误:powershell.exe-command“$t=”[DllImport(“user32.dll”,EntryPoint=“ShowWindow”)]公共静态外部bool ShowWindow(int handle,int state);”;add type-name win-member$t-namespace native;[native.win]::ShowWindow([System.Diagnostics.Process]::GetCurrentProcess()| Get Process).MainWindowHandle,0)“
这个问题仍然存在。例如,此命令给出错误:powershell.exe-command“$t=”[DllImport(“user32.dll”,EntryPoint=“ShowWindow”)]公共静态外部bool ShowWindow(int handle,int state);”;add type-name win-member$t-namespace native;[native.win]::ShowWindow([System.Diagnost