PowerShell--Define text, password, and user interaction processing

Source: Internet
Author: User

    • Powershell definition Text

    1. Using double quotes, you can let the variables in your own defined characters be replaced by the content;

$site = "飞苔博客 Powershell博客" $text = "$site $(get-date) $env:windir" $text

Output:
Flying Moss blog PowerShell blog 08/25/2012 18:49:41 c:windows

2. Using single quotation marks, you can make your own defined string output as is;

$text = ‘$fei $(tai) $env:windir 飞苔博客 (20+2012)‘ $text

Output:
$fei $ (Tai) $env: windir blog (20+2012)

3. Two special characters in the text, one is the prefix of the variable "$", the other is the inverse quotation mark "·" On the left side of the number key 1.

PS c:\> "Computer name: $env: ComputerName"
Computer Name:computer1

PS c:\> "Current date:$ (get-date)"
Current date:05/07/2015 09:47:11

4. PowerShell Escape character

In other programming languages, you prefer to use backslashes as escape characters, but instead of backslashes that play the escape character role in PowerShell, instead of the backslash, the inverted quotation marks in the backslash "'" string, special handling of the character immediately following it.

#使用单引号闭合字符串输出双引号 ‘The site of my blog is"www.mossfly.com"‘ #使用转义字符输出双引号 “My blog site is` "www.mossfly.com`"" #在字符串中输出换行符 “The site of my blog is `"www.mossfly.com`",`n飞苔博客"

Output:

The site of my blog is "www.mossfly.com"
My blog site is "www.mossfly.com"
The site of my blog is "www.mossfly.com",
Flying Moss Blog

5. PowerShell Escape Character Descriptor

Escape character Describe
' N Line break
' R Carriage return character
' t Tabs
' A Bell character
' B Backspace
"' Single quotation marks
' " Double quotes
' 0 Null
The anti-quote itself

6. Strings define multiple lines of text in PowerShell

@ "string" @ format defines multiple lines of text, especially longer text, which is fine, note that the start and end tags must be in a different row.

@" 这首诗用来评价陶渊明的诗歌再好不过了 一语天然万古新,豪华落尽见真淳。 南窗白日羲皇上,未害渊明是晋人。 "@

7. User interaction in PowerShell

If you want to prompt for user input, you can use Read-host

PS e:> $name =read-host "Please enter your username"
Please enter your user name: Mosser Lee
PS e:> "The user name you entered is: $name"
The user name you entered is: Mosser Lee

How do I parse the variables in read-host? To parse by expandstring method

PS e:> $inputPath =read-host "Please enter file path"
Please enter the file path: $env: windir
PS e:> $inputPath
$env: windir
PS e:> $ExecutionContext. invokecommand.expandstring ($inputPath)
C:windows

How do I convert an encrypted password to normal text?

PS e:> $pwd =read-host-assecurestring "Please enter your password"
Please enter the password: ******
PS e:> $pwd
System.Security.SecureString
PS e:> [Runtime.InteropServices.Marshal]::P trtostringauto ([Runtime.InteropServices.Marshal]:: Securestringtobstr ($PWD))
abc123

Asking for a user name and password

If you want to authorize a user to provide user credentials, you can use the get-credential command, the command will pop up a security dialog box, once the user has finished typing, will return a credential object containing the user name and password

PS e:> $cre =get-credential Mossserlee
PS e:> $cre
UserName Password
--------                        --------
Mossserlee System.Security.SecureString

How to easily get user credentials without requiring user interaction input?

Function get-domaincredential ()
{
$domain =get-domainname
$username = "$domain \administrator"
$password = "123456"
$cred = New-object system.management.automation.pscredential-argumentlist @ ($username, (Convertto-securestring- String $password-asplaintext-force))
Return $cred
}

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1643717

. PowerShell--Define text, password, and user interaction processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.