与关于如何正确构造参数参数的这个梦幻般的答案相关-Filter,我正在研究一个在PowerShell中大量使用Active Directory cmdlet的脚本,并且很难构造包含对象变量属性的过滤器参数.
该脚本基本上通过读取电子表格并根据需要处理每一行来管理通讯组 - 创建Active Directory联系人(如果它们不存在),将其添加或删除到适当的组等等.
电子表格存储在一个对象数组中,每一行都被视为一个对象,其中的列定义了对象属性.
例如,以下代码获取导入的电子表格的每一行,并尝试在Active Directory中查找匹配的联系人对象.
Foreach ($contact in $NOClist)
{
$contactobject = Get-ADObject -filter ("name -eq `"$contact.name`" -and Objectclass -eq `"contact`"") -SearchBase "$NOCContactPath" -Server $ADServer;
#... do some more stuff.
}
Run Code Online (Sandbox Code Playgroud)
问题在于按$contact.name字面意思进行评估,因此最终在Active Directory中搜索具有名称属性的联系人$contact.name.我已经试过其他的变化在前面参考答案,( ""$contact.name"",'$contact.name'和"' + $contact.name + '"),但他们都要么评估字面$contact.name或扔在一个语法错误.的字符.
我提出的hacky解决方法是将对象属性分配给变量并使用它,例如下面的,但这只是感觉很糟糕.
Foreach ($contact in $NOClist)
{
$contactname = $contact.name;
$contactobject = Get-ADObject -filter ("name -eq `"$contactname`" -and Objectclass -eq `"contact`"") …Run Code Online (Sandbox Code Playgroud) 我有一个类似于以下的文件夹系统:
Folder A
Folder.A.S01.E01.MP4
Folder.A.S01.E02.MP4
Folder.A.S02.E01.TEST.THIS.MP4
Folder.A.S02.E01.TEST.THIS.MP4
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以遍历文件夹并重命名文件,如下所示:
Folder A
Folder A S01E01.MP4
Folder A S01E02.MP4
Folder A S02E01.MP4
Folder A S02E01.MP4
Run Code Online (Sandbox Code Playgroud)
Which I understand is just removing the . in the name of all files in the folder.
I have tried the following, but it removes the . file extension as well and "breaks" the file.
cd 'C:\Users\ME\Desktop\HERE'
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "."} |
foreach {
$New=$_.name.Replace("."," ")
Rename-Item -path $_.Fullname -newname $New -passthru …Run Code Online (Sandbox Code Playgroud) 我目前在SQL Server配置管理器中显示TCP端口显示为空白.我需要弄清楚如何使用PowerShell将其更改为端口1433.我已经能够运行它来实际获取值:
$smo = 'Microsoft.SqlServer.Management.Smo.'
$wmi = new-object ($smo + 'Wmi.ManagedComputer')
$uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value +
"']/ServerInstance[@Name='SQLSERVER']/ServerProtocol[@Name='Tcp']"
$Np = $wmi.GetSmoObject($uri)
$NPProp = $Np.IPAddresses.where({$_.Name -eq 'IPALL'})
$NPPropTCP = $NPProp.IPAddressProperties.where({$_.Name -eq 'TcpPort'})
$NPPropTCP.Value
Run Code Online (Sandbox Code Playgroud)
但是,当我跑:
$NPPropTCP[1].Value = '1433'
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法在此对象上找到属性"值".验证该属性是否存在且可以设置.
在行:20 char:1
+ $ NPPropTCP [1] .Value ='1433'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo :InvalidOperation:(:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId:PropertyNotFound
我觉得这可能很简单,但我被困了......
我正在整合两个通过CSV文件相互通信的软件,但是App A的输出文件需要进行一些后期处理才能让App B拿起它.
下面的代码将数据附加到以"C"开头的行,同时丢弃现在的其他三种可能性,因为这些部分将在以后添加.
$ _> null是我提出的,因为我找不到会删除IF语句中的行的代码片段.它似乎有效,但我想确定它是否正确.TIA
$original = "input.txt"
$destination = "output.txt"
$tempfile = [IO.Path]::GetTempFileName()
get-content $original | foreach-object {
if ($_ -match "^C,") {
$_ + ",append this" >> $tempfile
}
elseif ($_ -match "^J,") {
$_ > null
}
elseif ($_ -match "^B,") {
$_ > null
}
elseif ($_ -match "^O,") {
$_ > null
}
else {
$_ >> $tempfile
}
}
copy-item $tempfile $destination
remove-item $tempfile
Run Code Online (Sandbox Code Playgroud) 我在一个文件夹中有一系列文件名称: - myFile201801010703.file
我正在尝试解析文件夹中每个文件名的yyyymmdd部分,并根据日期对数组进行排序.
所以,如果我有以下文件:
它会将它们分类为一个数组:
我有一个适用于名称中包含时间戳的文件的进程,但是只能在日期工作时无法调整它:
# RegEx pattern to parse the timestamps
$Pattern = '(\d{4})(\d{2})(\d{2})*\' + ".fileExtension"
$FilesList = New-Object System.Collections.ArrayList
$Temp = New-Object System.Collections.ArrayList
Get-ChildItem $SourceFolder | ForEach {
if ($_.Name -match $Pattern) {
Write-Verbose "Add $($_.Name)" -Verbose
$Date = $Matches[2],$Matches[3],$Matches[1] -join '/'
$Time = $Matches[4..6] -join ':'
[void]$Temp.Add(
(New-Object PSObject -Property @{
Date = [datetime]"$($Date) $($Time)" #If I comment out $($Time)it doesn't work.
File = $_
}
))
}
} …Run Code Online (Sandbox Code Playgroud) 执行该行时,Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.htmlPowerShell throws抛出WebCmdletResponseException。我如何获得有关它的更多信息,这可能是什么原因造成的?虽然我可以使用Python成功获取页面的内容,但是在PowerShell中会引发异常。
完全例外:
Run Code Online (Sandbox Code Playgroud)Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. At line:1 char:1 + Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
我正在尝试使用PowerShell对目录中的CSV文件集合进行排序,然后将结果导出到新的单个CSV文件.
我的代码执行没有错误,但结果没有正确排序.
我的源数据看起来像这样(这是一个测试样本):
"TimeCreated","TargetUserSID","UserName","Domain","Message","Remark"
"04.12.2017 13:56:56","S-1-5-21-66666666-777777777-888888888-99999","User123","testlab.internal","Benutzerinitiierte Abmeldung","User initiated Logoff."
"04.12.2017 13:56:48",,"User123","testlab.internal","Ein Konto wurde erfolgreich angemeldet","Workstation unlocked"
"04.12.2017 13:56:48",,"User123","testlab.internal","Ein Konto wurde erfolgreich angemeldet","Workstation unlocked"
"04.12.2017 13:56:34",,"User123","testlab.internal","Fehler beim Anmelden eines Kontos","Failed Login !"
"04.12.2017 10:29:49",,"User123","testlab.internal","Ein Konto wurde erfolgreich angemeldet","Interactive (logon at keyboard and screen of system)"
"04.12.2017 10:29:49",,"User123","testlab.internal","Ein Konto wurde erfolgreich angemeldet","Interactive (logon at keyboard and screen of system)"
"04.12.2017 10:27:27","S-1-5-21-66666666-777777777-888888888-99999","User123","testlab.internal","Benutzerinitiierte Abmeldung","User initiated Logoff."
"08.11.2017 09:24:58",,"","","Fehler beim Anmelden eines Kontos","Failed Login !"
"08.11.2017 09:23:14",,"","","Fehler beim Anmelden eines Kontos","Failed Login !"
"04.12.2017 10:25:35",,"User123","testlab.internal","Ein …Run Code Online (Sandbox Code Playgroud) 我有一个运行第三方应用程序的cmdlet并输出一个表3列一个简单的脚本- ,Name,。Result 仅包含三个值中的一个:,或。JobNameResultSuccessWarningFailed
输出:
Name Result JobName
---- ------ -------
server1 Success servers-A
server2 Success servers-A
server3 Warning servers-A
server4 Success servers-A
server5 Warning servers-B
server6 Success servers-B
server7 Failed servers-C
server8 Failed servers-C
Run Code Online (Sandbox Code Playgroud)
我想要做的排序是由表格Result列,但在下面的自定义命令(重要性排序): ,Failed,Warning然后Success。
例
Name Result JobName
---- ------ -------
server7 Failed servers-C
server8 Failed servers-C
server3 Warning servers-A
server5 Warning servers-B
server1 Success servers-A
server2 Success servers-A
server4 Success …Run Code Online (Sandbox Code Playgroud) I want to calculate the following in Powershell
$x = 100
$y = 25
result = x * y%
Run Code Online (Sandbox Code Playgroud)
Should be really simple, but I can't seem to come up with the right way to calculate the correct result in Powershell
注意:人们已将此标记为另一个问题的重复,但事实并非如此。我的virtualenv有一些问题,我无法解决。它可能与Visual Studio的设置方式有关。
尝试在Windows上激活虚拟环境时遇到问题。你如何执行$ venv\Scripts\activate?这应该是从命令提示符还是Powershell?我已经使用Visual Studio作为我的IDE。它为您创建了一个具有基本烧瓶应用程序的VS解决方案。在创建应用程序的过程中,它会要求您创建一个虚拟环境。它在类似于本教程所示目录的目录中创建该虚拟环境。\venv\Scripts退出,但没有名为“激活”的文件或可执行文件。
这是Scripts文件夹的内容:
api-ms-win-core-console-l1-1-0.dll api-ms-win-core-console-l1-1-0.dll
api-ms-win-core-debug-l1-1-0.dll
api-ms-win-core-errorhandling-l1-1-0.dll
api-ms-win-core-file-l1-1-0.dll api-ms-win-core-file-l1-2-0.dll
api-ms-win-core-file-l2-1-0.dll api-ms-win-core-file-l2-1-0.dll
api-ms-win-core-heap-l1-1-0.dll api-ms-win-core-heap-l1-1-0.dll
api-ms-win-core-libraryloader-l1-1-0.dll
api-ms-win-core-localization-l1-2-0.dll
api-ms-win-core-Memory-l1-1-0.dll api-ms-win-core-namedpipe-l1-1-0.dll
api-ms-win-core-processenvironment-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-1.dll
api-ms-win-core-profile-l1-1-0.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-string-l1-1-0.dll api-ms-win-core-sync-l1-1-0.dll
api-ms-win-core-synch-l1-2-0.dll api-ms-win-core-synch-l1-2-0.dll
api-ms-win-core-timezone-l1-1-0.dll api-ms-win-core-timezone-l1-1-0.dll
api-ms-win-crt-conio-l1-1-0.dll api-ms-win-crt-con1-1-.dll
api-ms-win-crt-environment-l1-1-0.dll
api-ms-win-crt-filesystem-l1-1-0.dll api-ms-win-crt-file-l1-1-0.dll
api-ms-win-crt-locale-l1-1-0.dll api-ms-win-crt-math-l1-1-0.dll
api-ms-win-crt-multibyte-l1-1-0.dll api-ms-win-crt-private-l1-1-0.dll
api-ms-win-crt-process-l1-1-0.dll api-ms-win-crt-process-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll api-ms-win-crt-l1-1-0.dll
api-ms-win-crt-time-l1-1-0.dll api-ms-win-crt-utility-l1-1-0.dll
concrt140.dll msvcp140.dll pyexpat.pyd python.exe python3.dll
python36.dll pythoncom36.dll pythonw.exe pywintypes36.dll select.pyd
sqlite3.dll tcl86t.dll tk86t.dll ucrtbase.dll unicodedata.pyd
vccorlib140.dll vcomp140.dll vcruntime140.dll winsound.pyd
xlwings32.dll xlwings64.dll
_asyncio.pyd
_bz2.pyd
_ctypes.pyd
_ctypes_test.pyd
_decimal.pyd …
powershell ×10
csv ×1
if-statement ×1
object ×1
properties ×1
python ×1
sorting ×1
sql ×1
ssl ×1
tls1.2 ×1
variables ×1
virtualenv ×1
windows ×1