小编Ans*_*ers的帖子

将密码传递给-credential

我正在尝试登录计算机.我一直在玩各种版本,并确定我过去的问题是当我不知道我真正想做什么的时候.

我发现在运行脚本时我在错误的PC上.

当我现在在正确的PC上运行脚本时,以下代码要求我输入密码.

gwmi win32_service –credential domain\username –computer PC#
Run Code Online (Sandbox Code Playgroud)

我上面的当前脚本是否有办法在没有用户输入的情况下强制使用用户名和密码?我必须为100台PC执行此操作,因此我想循环遍历所有这些PC而无需用户输入密码100次.


我尝试过以下操作:

$Username = 'domain\username'
$Password = 'password'

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$SecureString = $pass
# Users you password securly
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString –computer PC#
Run Code Online (Sandbox Code Playgroud)

但是,我得到了一个错误 A parameter cannot be found that matches parameter name 'computer'.

还尝试过:

$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString 
# Sets yous credentials to be used
#$RemoteConn = New-PSSession -ComputerName "PC#" -Credential $MySecureCreds -Authentication default
Run Code Online (Sandbox Code Playgroud)

但RemoteConn不起作用

powershell credentials

12
推荐指数
2
解决办法
6万
查看次数

在现有IE实例中打开选项卡

$ie = New-Object -com internetexplorer.application
Run Code Online (Sandbox Code Playgroud)

每次我用这个对象打开一个新网站(即每次脚本运行时)都会在新的IE窗口中打开,我不希望它这样做.我希望它在一个新的选项卡中打开,但在以前打开的IE窗口中也是如此.我想在下次运行脚本时重用此对象.我不想创建一个新对象

那么有什么方法可以检查Internet Explorer的实例并重用其实例???

我试过这个解决方案:

首先,您必须附加到已经运行的Internet Explorer实例:

$ie = (New-Object -COM "Shell.Application").Windows() `
    | ? { $_.Name -eq "Windows Internet Explorer" }
Run Code Online (Sandbox Code Playgroud)

然后导航到新URL.打开该URL的位置是通过Flags参数控制的:

$ie.Navigate("http://www.google.com/", 2048)
Run Code Online (Sandbox Code Playgroud)

但是我无法navigate在这个新创建的对象上调用方法$ie.

powershell internet-explorer

11
推荐指数
2
解决办法
2万
查看次数

反向智能搜索(reverse-i-search),如何获得以前的结果?

在终端(Ctrl+ R)上进行反向智能搜索时,我可以继续按Ctrl+ R返回"历史记录"并获取包含我的搜索字符串的旧命令.

如何返回到我已经显示的结果(以前的搜索结果)?

bash terminal command-line

11
推荐指数
1
解决办法
4765
查看次数

从短文件名获取完整路径和长文件名

我有一个很好的控制台文件管理器(Senh Liu的eXtreme),它将短路径/文件名作为变量传递给menu.bat.

如何生成完整的文件夹名称+长文件名?

例:

  • 输入变量="P:\ MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL"
  • target variable ="P:\ MyPrograms\SHELLS\zBackup\RefsToMyData.bal"

我尝试过以下方法:

  • SET my_file=%~2:

    echo %my_file% 生产: "P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL"

  • FOR /F "tokens=* USEBACKQ" %%F IN (`dir /B %2`) DO SET my_file=%%~fF:

    echo %my_file% 生产: "P:\MYPROG~1\SHELLS\zBackup\RefsToMyData.bal"

  • FOR /F "tokens=* USEBACKQ" %%F IN (`dir /B %2`) DO SET my_file=%%~dpnxF:

    echo %my_file% 生产: "P:\MYPROG~1\SHELLS\zBackup\RefsToMyData.bal"

batch-file

11
推荐指数
2
解决办法
2704
查看次数

将多个命令发送到外部程序

我们正在尝试编写一个调用外部应用程序的PowerShell脚本 - 一个Redis客户端(redis-cli.exe) - 然后向该.exe发送多个命令.发送如下所示的单个命令没有问题:

& redis-cli -h localhost -p 6379 SMEMBERS someKey
Run Code Online (Sandbox Code Playgroud)

问题是,这将启动Redis客户端,发出单个命令,关闭客户端,然后将控制权返回给PowerShell.我们需要在事务中发出多个命令.例如,以下是我们要发送给客户端的命令:

MULTI
DEL someKey
DEL someSet
EXEC
Run Code Online (Sandbox Code Playgroud)

Redis客户端支持将LUA脚本字符串作为命令发送,但遗憾的是,这不支持MULTI/EXEC事务命令.换句话说,我们需要能够发出如上所列的多个命令.

powershell scripting redis

11
推荐指数
1
解决办法
759
查看次数

%{$ _.Key1}是什么意思?

在为HDInsight编程时,我遇到了类似的行

$storageAccountKey = Get-AzureRmStorageAccountKey 
    -ResourceGroupName $resourceGroupName 
    -Name $storageAccountName 
    |  %{ $_.Key1 }
Run Code Online (Sandbox Code Playgroud)

我理解$_是指Get-AzureRmStorageAccountKey命令的结果.但究竟是什么意思%{}

powershell hdinsight

11
推荐指数
1
解决办法
8226
查看次数

将base64字符串转换为file

我正在尝试将base64字符串转换回原始文件.我尝试导出这些文件的应用程序只允许我导出base64字符串.此导出返回base64字符串和文件类型.

如何将这些字符串转换回原始文件?我一直在尝试这样的事情,但我认为这不适用于不同类型的文件?

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($file)) |
    Out-File C:\ID\document.$($extension)
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供一些关于如何做到这一点的想法吗?

powershell base64 file

11
推荐指数
1
解决办法
1万
查看次数

dig(DNS Lookup)在Windows上指定DNS服务器

在Linux中,我将使用dig以下命令指定DNS服务器127.0.0.1:

dig google.com @127.0.0.1
Run Code Online (Sandbox Code Playgroud)

我为windows(choco install bind-toolsonly)安装了Bind工具.我该如何运行相同的命令?我收到以下错误:

PS C:\Users\jhilden> dig google.com @127.0.0.1
At line:1 char:21
+ dig google.com @127.0.0.1
+                     ~
Missing property name after reference operator.
At line:1 char:16
+ dig google.com @127.0.0.1
+                ~~~~
The splatting operator '@' cannot be used to reference variables in an
expression. '@127' can be used only as an argument to a command. To
reference variables in an expression use '$127'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + …

dns powershell dig

11
推荐指数
2
解决办法
2万
查看次数

Invoke-WebRequest:无法绑定参数'Headers'

我试图在powershell中执行curl命令:

curl --user bitcoinipvision --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "move", "params": ["acc-1", "acc-2", 6, 5, "happy birthday!"] }' -H 'content-type: application/json;' http://localhost:18332/
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个错误,问题是什么?

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the
"content-type: application/json;" value of type "System.String" to type
"System.Collections.IDictionary".
At line:1 char:158
+ ... 5, "happy birthday!"] }' -H 'content-type: application/json;' http:// ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

powershell curl

11
推荐指数
2
解决办法
2万
查看次数

管道和 foreach 循环

最近,我一直在使用 PowerShell,我注意到在使用管道和foreach循环时出现了一些我无法理解的奇怪行为。

这个简单的代码可以工作:

$x = foreach ($i in gci){$i.length}
$x | measure -max
Run Code Online (Sandbox Code Playgroud)

说得通。

但这段代码不会:

foreach ($i in gci){$i.length} | measure -max
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

$x = foreach ($i in gci){$i.length}
$x | measure -max
Run Code Online (Sandbox Code Playgroud)

这两种方法有什么区别,为什么第二种方法会失败?

powershell powershell-2.0

10
推荐指数
1
解决办法
1万
查看次数