使用参数将Powershell转换为exe

时间:2017-03-29 10:42:39

标签: windows powershell networking exe

我有一个PowerShell脚本,如下所示:

param(
[string]$fullpath,
[string]$username,
[string]$password,
[string]$driveletter = "P:",
[bool]$storedrive = $False)

$net = new-object -ComObject WScript.Network

$net.MapNetworkDrive($driveletter, $fullpath, $storedrive, $username, $password)

我尝试将其转换为PS2EXE的可执行文件,但工作正常。

如果我尝试使用name.exe -fullpath "\\192.168.1.120\share" -username "administrator" -password "test123"运行.exe,我会收到错误消息Networkname not found

如果我将以下代码编译为可执行文件

param(
[string]$fullpath = "\\192.168.1.120\share",
[string]$username = "administrator",
[string]$password ="test123",
[string]$driveletter = "P:",
[bool]$storedrive = $False)

$net = new-object -ComObject WScript.Network

$net.MapNetworkDrive($driveletter, $fullpath, $storedrive, $username, $password)

...并运行.exe - 它可以工作,我得到了资源管理器中显示的网络驱动器....

为什么这不起作用的任何想法?

此致 马库斯

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,现在我正在使用这个脚本

param(
[string]$driveletter,
[string]$fullpath,
[string]$username,
[string]$password)

$net = new-object -ComObject WScript.Network

$net.MapNetworkDrive($driveletter, $fullpath, $false, $username, $password)

我将其转换为.exe 这很好用。 .exe使用以下"参数"

run.exe "P:" "\\192.168.1.120\share" "administrator" "test123"

转换布尔值存在一些问题。但是,它现在按我想要的方式工作。