我是蝙蝠脚本的新手,我正在阅读一个bat文件来设置eclipse的环境,我看到以下内容并没有理解代码.有人可以评论代码并帮助我.
:::::::: Check if the our working directory contains spaces
set working_dir_no_spaces=%working_dir%
set working_dir_no_spaces=%working_dir_no_spaces: =%
:::::::: If we've got a space, then quit here.
if not "%working_dir_no_spaces%"=="%working_dir%" (
echo ------------------------------------------------------------
echo Your directory path contains a space.
echo Try putting this package without a space in the path
echo ------------------------------------------------------------
echo Recommended location is your C drive itself
echo Common mistake is when people put this at their desktop
echo and unfortunately their name has a space :(
echo ------------------------------------------------------------ …
Run Code Online (Sandbox Code Playgroud) 我有一个try..catch
声明,但它没有被捕获,PS v4。
Function ReadFile ([string] $configfile) {
try {
[xml]$script:fileInfo = Get-Content $configFile
} catch {
Write-Host $_.Exception.Message
}
}
Run Code Online (Sandbox Code Playgroud)
它永远不会捕获,但它在控制台中出错?下面是控制台错误:
Get-Content : 找不到路径“C:\test.xml”,因为它不存在。 在 C:\test.ps1:3 字符:29 + [xml]$script:fileInfo = Get-Content $configFile + CategoryInfo : ObjectNotFound: (C:\test.xml:String) [Get-Content], ItemNotFoundException + FullQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
我需要PowerShell的数据类型来为键添加不同的值.我应该选择哪种类型?
数据如下:
+--------+--------+--------+--------+ | Serv1 | Serv2 | Serv3 | Serv4 | | -------+--------+--------+------- | | User1 | User2 | User3 | User4 | | User3 | User1 | User2 | User4 | | User7 | User8 | User9 | ------ | +--------+--------+--------+--------+
我正在尝试将数据打包到对象中,然后再显示它们ConvertTo-Json
.下面的测试用例完美展示了我如何处理数据以及出现了什么问题:
$array = @("a","b","c")
$data = @{"sub" = @{"sub-sub" = $array}}
$output = @{"root" = $data}
ConvertTo-Json -InputObject $data
ConvertTo-Json -InputObject $output
Run Code Online (Sandbox Code Playgroud)
输出(为清晰起见,手动格式化):
{ "sub": { "sub-sub": [ "a", "b", "c" ] }}
{ "root": { "sub": { "sub-sub": "a b c" } }}
Run Code Online (Sandbox Code Playgroud)
有没有什么办法来分配$data
给$output
没有这个怪异的隐式转换?
我正在查询大约280台XenApp服务器.这是我的疑问.
$bootupMemory = gwmi -Query "SELECT * FROM Win32_OperatingSystem" -ComputerName $srv
#$cpuLoad = gwmi -Query "SELECT * FROM Win32_Processor" -ComputerName $srv
#$tSessions = gwmi -Query "SELECT * FROM Win32_TerminalService" -ComputerName $srv
$ima = gwmi -Query "SELECT * FROM Win32_Service WHERE name='imaservice'" -ComputerName $srv
$mfcom = gwmi -Query "SELECT * FROM Win32_Service WHERE name='mfcom'" -ComputerName $srv
$ctxPrintMgr = gwmi -Query "SELECT * FROM Win32_Service WHERE name='cpsvc'" -ComputerName $srv
$msmqstatus = gwmi -Query "SELECT * FROM Win32_Service WHERE name='msmq'" -ComputerName $srv
$cDrive …
Run Code Online (Sandbox Code Playgroud) 我的目标是在目录中递归搜索所有包含正则表达式的文件,并且要牢记速度。然后输出到CSV,该CSV的一列包含完全匹配项,另一列显示找到它们的文件。感谢用户woxxom,我开始使用了,IO.File
因为它显然比使用快得多Select-String
。
这是我长期从事的项目,并且可以通过Select-String
和使用完成Export-Csv
,但这是一个相当缓慢的过程。
对我的新尝试遗漏了什么想法?
$ResultsCSV = "C:\TEMP\Results.csv"
$Directory = "C:\TEMP\examples"
$RX = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.|dot|\[dot\]|\[\.\])){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
$TextFiles = Get-ChildItem $Directory -Include *.txt*,*.csv*,*.rtf*,*.eml*,*.msg*,*.dat*,*.ini*,*.mht* -Recurse
$out = [Text.StringBuilder]
foreach ($FileSearched in $TextFiles) {
$text = [IO.File]::ReadAllText($FileSearched)
foreach ($match in ([regex]$RX).Matches($text)) {
if (!(Test-Path $ResultsCSV)) {
'Matches,File Path' | Out-File $ResultsCSV -Encoding ASCII
$out.AppendLine('' + $match.value + ',' + $FileSearched.fullname)
$match.value | Out-File $ResultsCSV -Encoding ascii -Append
$FileSearched.Fullname | Out-File $ResultsCSV -Encoding ascii -Append
$out.ToString() | Out-File $ResultsCSV -Encoding …
Run Code Online (Sandbox Code Playgroud) 我的PowerShell CSV工具有一个奇怪的问题.我试着写一个小的支票,过滤掉某些名字和字符.这些名称/字符在这样的文本文件中:
XXX nana YYY ... DDD
我做的检查是这样的:
$reader = [System.IO.File]::OpenText($fc_file.Text)
try {
for() {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
# process the line
Import-Csv $tempfile -Delimiter $delimeter -Encoding $char |
where {$_.$fc_suchfeld -notmatch $line} |
Export-Csv $tempstorage -Delimiter $delimeter -Encoding $char -NoTypeInfo
Run Code Online (Sandbox Code Playgroud)
它工作得很好,直到3点的线.此时几乎所有行都被删除.我怎么解决这个问题?
从用户获取2个日期,它的工作原理除外:如果两个日期都在2017年,则显然不应显示错误消息.
我手动将2016年改为2017年,看看它是否有所作为,但没有(使用Replace
).
这是验证码:
todays_date = Date()
todays_date = Replace(todays_date, "/", "-")
If m_date_01 < todays_date Or m_date_02 < todays_date Then
m_valid = False
m_message = m_message & "<li>dates cannot be in the past</li>"
End If
response.write(todays_date)
response.write(m_date_01)
response.write(m_date_02)
Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本,如果帐户超过 7 天,它将删除特定用户。
但是当用户被删除时,我的文件服务器上的 NTFS 权限仍然存在。
如何使用 PowerShell 删除特定用户的所有权限?
在下面的代码try
块中,我想重试最后三个命令以运行多次,然后继续执行catch
并finally
阻止。如果我们可以为第 5、6 和 7 行放置,则意味着一种重试。假设第 5 行应该运行 3 次,如果失败,则继续执行catch
和finally
。
try {
$hostcomputer = hostname
$IP = "10.x.x.x"
$pso = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck:$TRUE -ErrorAction Stop
$session = New-PSSession -Authentication Negotiate -ConnectionUri https://mail.test.com/powershell/?ExchClientVer=15.1 -ConfigurationName microsoft.exchange -SessionOption $pso -ErrorAction Stop
Import-PSSession $session -AllowClobber -ErrorAction Stop
} catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $Error
Send-MailMessage -From User1.test@test.com -To "User2@test.com" -Subject "DC2 - RPS Not Working" -SmtpServer smtp.test.net -Body "Error generated on $hostcomputer …
Run Code Online (Sandbox Code Playgroud) powershell ×8
arrays ×1
asp-classic ×1
batch-file ×1
casting ×1
ntfs ×1
regex ×1
string ×1
types ×1
vbscript ×1
wmi ×1
wql ×1
xenapp ×1