powershell - 如何在 powershell 中创建提示以询问用户在哪个服务器组上运行脚本
问题描述
美好的一天,我有这样的服务器
$libraryserver = ('192.168.0.3','192.168.0.4')
$dormetoryserver = ('192.168.1.15','192.168.1.16')
$teacherserver = ('192.168.1.110','192.168.1.112')
当用户运行 powershell 脚本时,应该有提示符或 arg - 比如:runscript.ps1 -library,之后 $servers 将从 libraryserver 列表中获取
foreach($server in $servers) {
# Destination UNC path changes based on server name
$destinationPath = "\\$server\D$\tmp\"
# Check that full folder structure exists and create if it doesn't
if(!(Test-Path $destinationPath)) {
# -Force will create any intermediate folders
New-Item -ItemType Directory -Force -Path $destinationPath
}
# Copy the file across
Copy-Item $sourcefile $destinationPath
}
请你帮助我好吗 ?谢谢,尝试搜索并失败
解决方案
声明一个只接受三个有效值之一的参数(您可以ValidateSet
为此使用属性),然后根据参数选择适当的组:
param(
[ValidateSet('Library', 'Dormitory', 'TeachersLounge')]
[string]$ServerGroup
)
$servers = @{
Library = '192.168.0.3','192.168.0.4'
Dormitory = '192.168.1.15','192.168.1.16'
TeachersLounge = '192.168.1.110','192.168.1.112'
}[$ServerGroup]
foreach($server in $servers){
# ...
}
推荐阅读
- android - 深色主题不适用于某些 recyclerview 项目
- javascript - 未捕获的类型错误:无法读取 todolist 的属性
- java - log4j - 写入文件名作为记录器名称
- sql - MS SQL:创建 GROUP BY 语句
- reactjs - 如何从firestore获取数据并使用react js将其设置到输入框
- python - 如何使用 Python 查找当前用户的系统主题颜色?
- javascript - 单击提交按钮后如何根据文本值调整文本框的大小?
- azure - 将 Azure 数据工厂上的数据管道从 SQL Server 复制到 Blob 存储
- typescript - Typescript 类型以接受映射到特定值类型的对象的键
- database - 我收到类似“表不能有超过 0 个物化视图,无法创建物化视图”的错误