我正在编写一些PowerShell代码以用于以后的项目。这是一个列表,用户可以从中选择一个项目,并将选择项分配给一个变量。我不确定如何控制字体大小,特别是对于列表框文本。
这是代码:
# Creates a window that prompts a user to select an item from a list
#Enables .NET Framework Classes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Creates the window prompt
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select an Item"
$objForm.Size = New-Object System.Drawing.Size(600,500)
$objForm.StartPosition = "CenterScreen"
# Defines keystrokes as inputs
#
# Sets Enter to set highlighted item to a variable
# Sets Esc to close windowed prompt
#
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}}) …Run Code Online (Sandbox Code Playgroud) 我对PowerShell -replace运算符在使用正则表达式时的工作方式感到困惑.我在线查找文档,但找不到比基本用法更详细的内容:它查找字符串,并将该字符串替换为另一个字符串(如果已定义)或什么都没有.大.
我想和这个问题中的人做同样的事情,用户想从复杂的字符串中提取一个简单的程序名.这是我试图复制的代码:
$string = '% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2.F500 G3I6.4Z-8.G3I6.4 G3R3.2X6.4F500 G91G0Z5. G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2. M99 %'
$program = $string -replace '^%\sO\d{4}\((.+?)\).+$','$1'
$program
SUB RAD MSD 50R III
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,输出字符串是用户想要的字符串,其他所有字符串都被过滤掉了.对我来说,唯一的区别是我想要一个由六位数字组成的字符串,而不是别的.但是,当我尝试在我的正则表达式的字符串上执行此操作时,我得到了这个:
$string2 = '1_123456_1'
$program2 = $string -replace '(\d{6})','$1'
$program2
1_123456_1
Run Code Online (Sandbox Code Playgroud)
没有变化.为什么会这样?我的代码应该是什么?此外,$1代码中使用了什么?
我无法解决为什么0..10直接传递时管道不能通过我的功能.
我希望管道0..10到以下功能将返回10个结果.
function a{param([parameter(ValueFromPipeline=$true)][int[]]$t)$t|%{$_+10}}
0..10 | a
Run Code Online (Sandbox Code Playgroud)
我不明白为什么以下只返回一个结果.
我预计会通过管道传输一个值块并将其$t | %{$_+10}分解,对它们进行操作并将它们返回到输出.
如果我执行以下操作,它会按预期工作
a(0..10)
Run Code Online (Sandbox Code Playgroud)
我不认为有什么不对,只是因为我不明白什么0..10是什么,并希望得到社区的一些帮助.
无论如何根据某些条件将管道数据发送到两个不同的路径,而不重新执行上游命令?这里的符号都错了,但这是我想看到的......
PS> Get-SomeInterestingData `
| ?{ <condition> } `
| %{ <positive> } `
| %{ <negative> }
Run Code Online (Sandbox Code Playgroud)
我可以用嵌套的管道/表达式做到这一点吗?
PS> Get-SomeInterestingData `
| %{
$_ | ?{ <condition> } | %{ <positive> } -or `
$_ | ?{ -not <condition> } | %{ <positive> }
}
Run Code Online (Sandbox Code Playgroud)
如果您无法直接访问数据或数据生成功能,但是您从管道接收它,该怎么办?比如说,作为脚本或函数param块的一部分,就像......
param(
[Parameter(ValueFromPipeline = $true)]
$InputObject,
)
Run Code Online (Sandbox Code Playgroud) 当以admin执行时,我需要在批处理文件中捕获powershell脚本的输出。
例:
ps1文件:
Write-Host "PS1 executed"
exit 1
Run Code Online (Sandbox Code Playgroud)
如果我在没有管理员访问权限的情况下执行Powershell脚本
NotAdminFile.bat:
@ECHO OFF
setlocal enableextensions
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
echo %ERRORLEVEL%
endlocal
Run Code Online (Sandbox Code Playgroud)
然后,输出是
PS1 executed
1
Run Code Online (Sandbox Code Playgroud)
还行吧。但是,当我通过管理员权限执行Powershell脚本时
AdminFile.bat:
@ECHO OFF
setlocal enableextensions
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "%~dpn0.ps1" ' -Verb RunAs}"
echo %ERRORLEVEL%
endlocal
Run Code Online (Sandbox Code Playgroud)
然后,输出为:
0
Run Code Online (Sandbox Code Playgroud)
我不要 你能帮我吗?
所以我的foreach循环有问题,我无法弄清楚自己在做什么错。
以下是我的代码示例:
$objectarray ##( So this variable contains a list of lists ie name + address) ##
Foreach ($object in $objectarray.name){
$objectid = $objectarray.Where({$_.name -eq "$object"}).id
$objectaddress = $objectarray.Where({$_.name -eq "$object"}).address
$objectprint = "$objectid" + ": " + "$objectaddress"
$objectprint
return 0
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是$ objectarray中有多个列表,每个列表都有其名称,id,地址等
但是它只打印第一个,而我只有第一个0作为返回值……尽管事实上它们很多
我必须查看我的前同事留下的一些脚本,我很好奇为什么他$a = $(Read-Host -Prompt "Write something")在参数部分使用而不是$a = (Read-Host -Prompt "Write something").
我已经在一个脚本中测试了它,但到目前为止还没有看到任何差异.我通过谷歌和StackOverFlow搜索但到目前为止还没有发现任何线索可能有什么不同.也许我的搜索方法很糟糕.
如何使用PowerShell获取Chrome版本?我尝试了以下查询:
(Get-ItemProperty 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome').DisplayVersion
Run Code Online (Sandbox Code Playgroud)
这没用.
我是电源外壳的新手.我正在尝试对名称中间有版本号的文件列表进行排序.
这是文件列表:
sample-file-1.1.0-patch.zip
sample-file-1.1.1-patch.zip
sample-file-1.1.2-patch.zip
sample-file-1.1.2.1-fix-patch.zip
sample-file-1.1.3-patch.zip
sample-file-1.1.3.1-fix-patch.zip
..
sample-file-1.1.15-patch.zip
sample-file-1.1.15.1-fix-patch.zip
sample-file-1.1.15.2-fix-patch.zip
..
sample-file-2.0.0-patch.zip
sample-file-2.0.1-patch.zip
..
Run Code Online (Sandbox Code Playgroud)
我会请你帮我分类逻辑.
谢谢
sorting powershell powershell-2.0 powershell-3.0 version-sort
我已经安装了vagrant并成功添加了vagrant ubuntu box.但是从项目目录运行"vagrant up"命令时,我收到了这个错误:
在%Path%变量中找不到可执行的"powershell"vagrant试图运行.这是一个错误.请验证此软件是否已安装并且在路径上.
我还安装了Putty客户端并对其进行了配置.
注意:"vagrant init"命令工作正常,它在目录中创建了一个vagrant文件.Vagrant版本:1.9.7
VagrantFile包含
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial32"
end
Run Code Online (Sandbox Code Playgroud)
添加到环境变量的路径:C:\ HashiCorp\Vagrant\bin
powershell ×10
windows ×3
.net ×1
batch-file ×1
cmd ×1
foreach ×1
regex ×1
registry ×1
sorting ×1
string ×1
vagrant ×1
version ×1
version-sort ×1
virtualbox ×1