标签: powershell

如何将所需参数传递给Powershell ISE中的脚本?

见标题.

我在脚本的头部指定了所需的参数:

param ($G_ARCHIVE = $(throw "Need file to upload!"),
       $G_LOGFILE = $(throw "Need logfile!"))
Run Code Online (Sandbox Code Playgroud)

当我想用Powershell ISE调试脚本时:我该如何填写这些参数?

powershell parameter-passing powershell-ise

69
推荐指数
3
解决办法
6万
查看次数

从快捷方式打开特定目录中的Powershell

听起来应该这么简单......我一定是傻瓜.

我想要的只是创建一个Windows快捷方式,将Powershell打开到一个特定的目录:

我正在使用目标:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
    -noexit -command {cd c:/path/to/open}
Run Code Online (Sandbox Code Playgroud)

把它作为文本吐出命令.

powershell shortcuts windows-7

69
推荐指数
6
解决办法
5万
查看次数

在PowerShell中更改目录

我的PowerShell提示符当前指向我的C驱动器(PS C:>).如何将目录更改为Q(PS Q:>)驱动器中的文件夹?我的Q盘中的文件夹名称是"我的测试文件夹".

powershell

69
推荐指数
5
解决办法
27万
查看次数

如何使用PowerShell拆分文本文件?

我需要将一个大的(500 MB)文本文件(一个log4net异常文件)拆分成可管理的块,例如100个5 MB的文件就可以了.

我认为这应该是PowerShell的公园散步.我该怎么做?

powershell

68
推荐指数
7
解决办法
12万
查看次数

Try/catch似乎没有效果

我是powershell的新手,我试图通过try/catch语句添加错误处理,但它们似乎并没有真正捕获错误.这是powershell v2 CP3.

$objComputer = $objResult.Properties;
$strComputerName = $objComputer.name
write-host "Checking machine: " $strComputerName

try
{
    $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
    foreach ($objItem in $colItems) 
    {
        write-host "Bank Label: " $objItem.BankLabel
        write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
        write-host "Caption: " $objItem.Caption
        write-host "Creation Class Name: " $objItem.CreationClassName      
        write-host
    }
}
Catch 
{
    write-host "Failed to get data from machine (Error:"  $_.Exception.Message ")"
    write-host
}
finally 
{ }  
Run Code Online (Sandbox Code Playgroud)

当它无法联系特定的机器时,我在控制台中得到它,而不是我的干净捕获消息:

Get-WmiObject : The RPC …

powershell

68
推荐指数
5
解决办法
9万
查看次数

使用PowerShell使用FTP上载文件

我想使用PowerShell将文件与FTP一起传输到匿名FTP服务器.我不会使用任何额外的包.怎么样?

脚本必须没有挂起或崩溃的风险.

ftp powershell

68
推荐指数
5
解决办法
15万
查看次数

Powershell Web请求,不会在4xx/5xx上抛出异常

我正在编写一个PowerShell脚本,需要发出Web请求并检查响应的状态代码.

我试过写这个:

$client = new-object system.net.webclient

$response = $client.DownloadData($url)
Run Code Online (Sandbox Code Playgroud)

以及:

$response = Invoke-WebRequest $url
Run Code Online (Sandbox Code Playgroud)

但只要网页的状态代码不是成功状态代码,PowerShell就会继续并抛出异常,而不是给我实际的响应对象.

即使加载失败,如何获取页面的状态代码?

powershell

68
推荐指数
3
解决办法
4万
查看次数

从Powershell脚本运行BAT文件的最安全方法

我无法获得PowerShell脚本直接执行bat文件.例如,这适用于命令行:

.\\my-app\my-fle.bat
Run Code Online (Sandbox Code Playgroud)

当我将此命令添加到脚本时,它输出:

The term '.\\my-app\my-file.bat' 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.
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下结果,结果相同:

& .\\my-app\my-fle.bat
& ".\\my-app\my-fle.bat"
\my-app\my-fle.bat
& \my-app\my-fle.bat
& "\my-app\my-fle.bat"
Run Code Online (Sandbox Code Playgroud)

注意:必须返回lastexitcode,因为我需要验证批处理是否成功.

powershell batch-file

68
推荐指数
4
解决办法
21万
查看次数

如何使用winrm将多台计算机添加到可信主机列表中

要从远程计算机在计算机上运行powershell命令,我们必须将远程计算机添加到主机的可信主机列表中.

我使用以下命令将机器A添加到机器B的可信主机:

winrm set winrm/config/client ‘@{TrustedHosts="machineA"}’
Run Code Online (Sandbox Code Playgroud)

如何添加更多机器说机器C,机器D到机器B的可信主机列表?

windows powershell hosts winrm

68
推荐指数
4
解决办法
15万
查看次数

在PowerShell中运行CMD命令

我遇到了一些关于运行PowerShell命令的问题.它所做的只是运行一个将在CMD提示窗口中运行的命令.

这是命令:

"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" PCNAME

我已经尝试了以下但没有成功(我尝试过多次迭代尝试并获得一个有效.语法可能都搞砸了):

$TEXT = $textbox.Text #$textbox is where the user enters the PC name.
$CMDCOMMAND = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"
Start-Process '"$CMDCOMMAND" $TEXT'
#iex -Command ('"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"' $TEXT)
Run Code Online (Sandbox Code Playgroud)

该命令将只打开SCCM远程连接窗口到用户在文本框中指定的计算机.

powershell command-line cmd

68
推荐指数
3
解决办法
29万
查看次数