简体   繁体   English

Powershell中的变量-将变量添加到脚本

[英]Variables in powershell - Adding variable to script

I am looking to write a script in PowerShell, however I want to set a variable and then have it fill the variable value in my script. 我希望在PowerShell中编写脚本,但是我想设置一个变量,然后让其填充脚本中的变量值。 To be more specific the variable I want to set is an IP address and I want to enter it into a script that pulls 3 files for me, as I manage a large number of computers. 更具体地说,我要设置的变量是IP地址,由于要管理大量计算机,因此我想将其输入到脚本中,该脚本可以为我提取3个文件。 Here is an example of one file from the script that I want run: 这是我要运行的脚本中一个文件的示例:

Copy-Item -Path '\\VariableIwantToEnterIPaddress\C$\ComputerZ\log\File.log' -Destination C:\Users\JayZ\Desktop

Where I put VariableIwantToEnterIPaddress I would place a variable then have it fill as an IP address. 我在放置VariableIwantToEnterIPaddress位置放置一个变量,然后将其填充为IP地址。 Either setting the variable first then running the script. 首先设置变量,然后运行脚本。 Or having PowerShell prompt me for a value then it run the script. 或者让PowerShell提示我输入值,然后运行脚本。

Is this possible, or is there a better method of doing this? 这可能吗,或者有更好的方法吗?

This should do the trick 这应该可以解决问题

$IPs = '10.10.0.1','10.10.0.2','10.10.0.3' #Enter your IP addresses here
foreach ($ip in $IPs) {
    Copy-Item "\\$($ip)\C$\ComputerZ\log\File.log" -Destination C:\Users\JayZ\Desktop
}

Edit: This should make it easier to enter in IP addresses, as many as you would like 编辑:这将使您更容易输入IP地址,只要您想要

function Copy-Files {
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$true,ValueFromPipeline)]
        [string]$ip
    )        
    process {
        Copy-Item "\\$($ip)\C$\ComputerZ\log\File.log" -Destination C:\Users\JayZ\Desktop
    }
}

Usage: 用法:

'10.10.0.1','10.10.0.2','10.10.0.3' | Copy-Files

The simplest way, in my opinion, is to use the Read-Host cmdlet. 我认为,最简单的方法是使用读取主机cmdlet。 Basically: 基本上:

$ipaddr = Read-Host -Prompt 'IP:'
Copy-Item "\\$ipaddr\C$\ComputerZ\log\File.log" -Destination C:\Users\JayZ\Desktop

By using Read-Host and the string .Split, you could loop through several IPs like this: 通过使用Read-Host和字符串.Split,您可以像这样遍历几个IP:

$ipaddr = Read-Host -Prompt 'IP List:'
$iplist = ipaddr.Split(" ")
foreach ($ip in $iplist) {
    Copy-Item "\\$ip\C$\ComputerZ\log\File.log" -Destination C:\Users\JayZ\Desktop
}

Then you just type in the IP addresses with a space between them when you run the script. 然后,在运行脚本时,只需输入IP地址,并在它们之间添加一个空格即可。

Use script parameters. 使用脚本参数。

MyScript.ps1 MyScript.ps1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $True, Position = 0)]
    [Alias('CN','IP')]
    [System.String[]]
    $ComputerName
)
ForEach ($CN in $ComputerName)
{
    Copy-Item -Path "\\$CN\C$\ComputerZ\log\File.log" -Destination C:\Users\JayZ\Desktop
}

The [Parameter()] attribute I've included forces the user to enter a CN/IP if omitted and allows you to not use named-parameters: 我包含的[Parameter()]属性强制用户输入CN / IP(如果省略),并允许您不使用命名参数:

PS C:\> MyScript.ps1 127.0.0.1

or 要么

PS C:\> MyScript.ps1 -ComputerName 127.0.0.1

When you omit: 省略时:

PS C:\> MyScript.ps1

cmdlet MyScript at command pipeline position 1
Supply values for the following parameters:
ComputerName: _

And lastly, to run against multiple PCs, use a comma-separated list: 最后,要在多台PC上运行,请使用逗号分隔的列表:

PS C:\> MyScript.ps1 127.0.0.1, 168.5.3.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM