Powershell 更改当前正在运行的脚本
有没有办法将脚本特定部分的文本添加到当前运行的脚本中 如果我有带选项的菜单:Powershell 更改当前正在运行的脚本,powershell,menu,Powershell,Menu,有没有办法将脚本特定部分的文本添加到当前运行的脚本中 如果我有带选项的菜单: 安装所有 添加项 退出 可以添加项目吗 学习使用powershell(批量的大量用户) 输入Add item时,会弹出一个读取主机,在长行####addwifi-wnm$USERINPUT之间添加一行,然后“重新启动”脚本 当前脚本: #cmd: title Add**** $host.ui.RawUI.WindowTitle = "Add Wi-Fi networks" #When Sh
- 安装所有
- 添加项
- 退出 可以添加项目吗
读取主机
,在长行####addwifi-wnm$USERINPUT
之间添加一行,然后“重新启动”脚本
当前脚本:
#cmd: title Add****
$host.ui.RawUI.WindowTitle = "Add Wi-Fi networks"
#When Show-Menu –Title 'SetupWi-Fi' is called
function Show-Menu
{
# NOTE if changing warible from somewhere else (Show-Menu -WARIBLE VALUE) then param part must be included
param (
[string]$Title = 'SetupWi-Fi'
)
Clear-Host
#cls and echo on @echo off
Write-Host "================ $Title ================"
Write-Host "a: Add Wi-Fi networks."
Write-Host "q: Quit."
}
#Do this until x
#For future shortening purposes
function addwifi
{
param (
[string]$wnm
#wnm= wifi name
)
netsh wlan add profile filename="$wnm.xml"
#for some reason (nice for this script) . stops the warible name
}
do
{
# call Show-Menu and optionally change varible: –Title 'Warible' changes the $title varible
Show-Menu
# makin varible chaase equal user input, placing Selection before it
$chaase = Read-Host "Selection:"
#switch according to the varible chaase
switch ($chaase)
{
'a' {
#'single quote' acts as echo, now executing commands of 'a' varible
'Adding Wi-Fi networks.'
$host.ui.RawUI.WindowTitle = "Adding Wi-Fi networks"
#note the upper function is called with warible
#add below here! #####################################################################
addwifi -wnm laptopidee
#add above here! #####################################################################
}
#close a execution
#close switch
}
#close do
}
#until x: selection == input q
until ($chaase -eq 'q')
对。使用外部文件作为要拉入的源。“添加项”菜单选项将创建另一个文件,以便在下次执行时读入
许多人使用批处理文件使用.ini文件来保存参数。类似的构造。一种可能性是使用在运行时替换的占位符,尽管我不确定它对更复杂的脚本的支持程度 例如,如果您有以下脚本:
$scriptPath = "$PsScriptRoot\$($MyInvocation.MyCommand.Name)"
$scriptContent = Get-Content "$PsScriptRoot\$($MyInvocation.MyCommand.Name)" -Raw
$newItem = Read-Host "Please enter new command"
##Placeholder
$scriptContent -replace "$([char]0x0023)$([char]0x0023)Placeholder", "$([char]0x0023)$([char]0x0023)Placeholder$([char]0x000D)$([char]0x000A)$newItem" |
Set-Content -Path $scriptPath
每次运行它时,系统都会提示您输入新命令,该命令将添加到##占位符
下方。因此,如果在提示时输入Get Process
,脚本将以如下方式结束在磁盘上:
$scriptPath = "$PsScriptRoot\$($MyInvocation.MyCommand.Name)"
$scriptContent = Get-Content "$PsScriptRoot\$($MyInvocation.MyCommand.Name)" -Raw
$newItem = Read-Host "Please enter new command"
##Placeholder
Get-Process
$scriptContent -replace "$([char]0x0023)$([char]0x0023)Placeholder", "$([char]0x0023)$([char]0x0023)Placeholder$([char]0x000D)$([char]0x000A)$newItem" |
Set-Content -Path $scriptPath
下次运行脚本时,系统将提示您输入一个新命令,该命令将添加到列表中,并且列表中已存在的所有命令都将被执行。我自己没有澄清,但问题是,在没有额外文件的情况下,您能做到这一点吗?您可以使用read host获取脚本中未提供的新信息,而且它仍然可以立即采取行动。但是,据我所知,新信息无法写回正在运行的脚本中。works:)我理解,为什么不应该正常使用它(为简单起见,构建脚本,多个,但主脚本要求管理员并允许powershell脚本,remotesigned然后按顺序打开所有安装脚本,因为我在销售笔记本电脑,尝试一些也不错,使用1个脚本(.cmd)(它还下载其他脚本)可以设置1台笔记本电脑..我不喜欢交换SSD破坏东西)