Powershell 将密码传递到-凭据

Powershell 将密码传递到-凭据,powershell,credentials,Powershell,Credentials,我正在尝试登录计算机。 我一直在玩各种版本的游戏,并确定我过去的问题是当我不知道我真正想做什么的时候 运行脚本时,我发现我的电脑不正确 当我现在在正确的PC上运行脚本时,以下代码要求我输入密码 gwmi win32_service –credential domain\username –computer PC# 我的当前脚本有没有办法在没有用户输入的情况下强制执行用户名和密码?我必须在100秒的PC上执行此操作,因此我希望在所有PC上循环,而用户不必输入100秒的密码 我试着做了以下几点:

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

运行脚本时,我发现我的电脑不正确

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

gwmi win32_service –credential domain\username –computer PC#
我的当前脚本有没有办法在没有用户输入的情况下强制执行用户名和密码?我必须在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#
但是,我遇到一个错误,
找不到与参数名称“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

但是RemoteConn没有工作

哇,多亏了

所以我没有意识到我可以使用
$mysecureds
作为
-凭证

答复:

$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 

gwmi win32_service –credential $MySecureCreds –computer PC#
Windows PowerShell 版权所有(C)2015微软公司。版权所有

PS C:\Users\joshua>获取帮助获取凭据-已满

名字 获取凭证

概要 基于用户名和密码获取凭据对象

gwmi win32_service –credential domain\username –computer PC#
语法 获取凭证[-凭证][]

Get-Credential [[-UserName] <String>] -Message <String> [<CommonParameters>]
参数 -凭证 指定凭据的用户名,例如“User01”或“Domain01\User01”。参数名 (“凭证”)是可选的

    When you submit the command, you are prompted for a password.

    Starting in Windows PowerShell 3.0, if you enter a user name without a domain, Get-Credential no longer
    inserts a backslash before the name.

    If you omit this parameter, you are prompted for a user name and a password.

    Required?                    true
    Position?                    1
    Default value                None
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Message <String>
    Specifies a message that appears in the authentication prompt.

    This parameter is designed for use in a function or script. You can use the message to explain to the user why
    you are requesting credentials and how they will be used.

    This parameter is introduced in Windows PowerShell 3.0.

    Required?                    true
    Position?                    named
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false

-UserName <String>
    Specifies a user name. The authentication prompt requests a password for the user name. By default, the user
    name is blank and the authentication prompt requests both a user name and password.

    When the authentication prompt appears in a dialog box, the user can edit the specified user name. However,
    the user cannot change the user name when the prompt appears at the command line. When using this parameter in
    a shared function or script, consider all possible presentations.

    This parameter is introduced in Windows PowerShell 3.0.

    Required?                    false
    Position?                    1
    Default value                None (blank)
    Accept pipeline input?       false
    Accept wildcard characters?  false

<CommonParameters>
    This cmdlet supports the common parameters: Verbose, Debug,
    ErrorAction, ErrorVariable, WarningAction, WarningVariable,
    OutBuffer, PipelineVariable, and OutVariable. For more information, see
    about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
输出 System.Management.Automation.PSCredential

注释

相关链接
联机版本:

新对象不接受名为Computer的参数。要查看其所有参数,请执行man New OBJECTS。第一个代码
gwmi win32_服务–凭证域\用户名–计算机PC
是否有办法调用密码@DanLI在上面还有另一个新的对象调用测试-update@DanLdepends取决于您的情况,但是您可以请求一次凭据,然后重新使用它们。请不要在主题中放置标记(或伪标记)。如果你的问题的答案提供了解决方案,考虑一下。获得帮助是你的朋友:Dhope TAT SelFractTink的TIGIE是练习背后的重点,你不打算把你的管理员(域)密码放在清晰的地方:我的程序的最后一点是做失败的登录。所以程序的位置是在实际域的位置,所以使用它的每个人都知道域。另外,密码和用户名将是一个带有假密码的测试设备,用于登录失败。因此,我的设置很好:)@JaquelineVanekMissed a
因此编辑了我的代码片段lol。在
username
XD回答了我自己的问题后,我才意识到大部分代码都是红色的,我的分数仍然是-1。smh永远不能用这个网站取胜。我下面的答案和你的过程是一样的。几分钟前发现:)谢谢!
    You cannot pipe input to this cmdlet.
    Get-Credential returns a credential object.
    You can use the PSCredential object that Get-Credential creates in cmdlets that request user authentication,
    such as those with a Credential parameter.
    By default, the authentication prompt appears in a dialog box. To display the authentication prompt at the
    command line, add the ConsolePrompting registry entry
    (HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\ConsolePrompting) and set its value to True. If the
    ConsolePrompting registry entry does not exist or if its value is False, the authentication prompt appears in
    a dialog box. For instructions, see the examples.

    The ConsolePrompting registry entry works in the Windows PowerShell console, but it does not work in all host
    programs. For example, it has no effect in the Windows PowerShell Integrated Scripting Environment (ISE). For
    information about the effect of the ConsolePrompting registry entry, see the help topics for the host program.
    The Credential parameter is not supported by all providers that are installed with Windows PowerShell.
    Beginning in Windows PowerShell 3.0, it is supported on selected cmdlet, such as the Get-WmiObject and
    New-PSDrive cmdlets.

-------------------------- EXAMPLE 1 --------------------------

PS C:\>$c = Get-Credential


This command gets a credential object and saves it in the $c variable.

When you enter the command, a dialog box appears requesting a user name and password. When you enter the requested
information, the cmdlet creates a PSCredential object representing the credentials of the user and saves it in the
$c variable.

You can use the object as input to cmdlets that request user authentication, such as those with a Credential
parameter. However, some providers that are installed with Windows PowerShell do not support the Credential
parameter.

-------------------------- EXAMPLE 2 --------------------------

PS C:\>$c = Get-Credential
PS C:\>Get-WmiObject Win32_DiskDrive -ComputerName Server01 -Credential $c


These commands use a credential object that the Get-Credential cmdlet returns to authenticate a user on a remote
computer so they can use Windows Management Instrumentation (WMI) to manage the computer.

The first command gets a credential object and saves it in the $c variable. The second command uses the credential
object in a Get-WmiObject command. This command gets information about the disk drives on the Server01 computer.

-------------------------- EXAMPLE 3 --------------------------

PS C:\>Get-WmiObject Win32_BIOS -ComputerName Server01 -Credential (Get-Credential -Credential Domain01\User01)


This command shows how to include a Get-Credential command in a  Get-WmiObject command.

This command uses the  Get-WmiObject cmdlet to get information about the BIOS on the Server01 computer. It uses
the Credential parameter to authenticate the user, Domain01\User01, and a Get-Credential command as the value of
the Credential parameter.





-------------------------- EXAMPLE 4 --------------------------

PS C:\>$c = Get-Credential -credential User01
PS C:\>$c.Username
\User01


This example creates a credential that includes a user name without a domain name. It demonstrates that
Get-Credential inserts a backslash before the user name.

The first command gets a credential with the user name User01 and stores it in the $c variable.

The second command displays the value of the Username property of the resulting credential object.





-------------------------- EXAMPLE 5 --------------------------

PS C:\>$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.",
"", "NetBiosUserName")


This command uses the PromptForCredential method to prompt the user for their user name and password. The command
saves the resulting credentials in the $Credential variable.

The PromptForCredential method is an alternative to using the Get-Credential cmdlet. When you use
PromptForCredential, you can specify the caption, messages, and user name that appear in the message box.

-------------------------- EXAMPLE 6 --------------------------

PS C:\>Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $true


This example shows how to modify the registry so that the user is prompted at the command line, instead of by
using a dialog box.

The command creates the ConsolePrompting registry entry and sets its value to True. To run this command, start
Windows PowerShell with the "Run as administrator" option.

To use a dialog box for prompting, set the value of the ConsolePrompting to false ($false) or use the
Remove-ItemProperty cmdlet to delete it.

The ConsolePrompting registry entry works in some host programs, such as the Windows PowerShell console. It might
not work in all host programs.

-------------------------- EXAMPLE 7 --------------------------

The first command saves the user account name in the $User parameter. The value must have the "Domain\User" or
"ComputerName\User" format.
PS C:\>$User = "Domain01\User01"

The second command uses the ConvertTo-SecureString cmdlet to create a secure string from a plain text password.
The command uses the AsPlainText parameter to indicate that the string is plain text and the Force parameter to
confirm that you understand the risks of using plain text.
PS C:\>$PWord = ConvertTo-SecureString –String "P@sSwOrd" –AsPlainText -Force

The third command uses the New-Object cmdlet to create a PSCredential object from the values in the $User and
$PWord variables.
PS C:\>$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord


This example shows how to create a credential object that is identical to the object that Get-Credential returns
without prompting the user. This method requires a plain text password, which might violate the security standards
in some enterprises.

-------------------------- EXAMPLE 8 --------------------------

PS C:\>Get-Credential -Message "Credential are required for access to the \\Server1\Scripts file share." -User
Server01\PowerUsers
Windows PowerShell Credential Request
Credential are required for access to the \\Server1\Scripts file share.
Password for user ntdev\juneb:


This command uses the Message and UserName parameters of the Get-Credential cmdlet. This command format is
designed for shared scripts and functions. In this case, the message tells the user why credentials are needed and
gives them confidence that the request is legitimate.

-------------------------- EXAMPLE 9 --------------------------

PS C:\>Invoke-Command -ComputerName Server01 {Get-Credential Domain01\User02}

Windows PowerShell Credential Request : Windows PowerShell Credential Request
Warning: This credential is being requested by a script or application on the SERVER01 remote computer. Enter your
credentials only if you
 trust the remote computer and the application or script requesting it.

Enter your credentials.
Password for user Domain01\User02: ***************



PSComputerName     : Server01
RunspaceId         : 422bdf52-9886-4ada-ab2f-130497c6777f
PSShowComputerName : True
UserName           : Domain01\User01
Password           : System.Security.SecureString


This command gets a credential from the Server01 remote computer. The command uses the Invoke-Command cmdlet to
run a Get-Credential command on the remote computer. The output shows the remote security message that
Get-Credential includes in the authentication prompt.
$pass="FooBoo"|ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object   System.Management.Automation.PsCredential('user@domain',$pass)
gwmi win32_service –credential $cred –computer $computer