我需要逐行向后写一个文件到另一个文件.我可以在PowerShell窗口中使它工作正常,但是当我尝试将它管道传输到它时Out-File,它不起作用.这是我的脚本:
Get-Content C:\Users\Administrator\Desktop\files.txt | ForEach-Object {
$text = $_.toCharArray()
[Array]::Reverse($text)
-join $text | Out-File 'C:\Users\Administrator\Desktop\reversed.txt'
}
Run Code Online (Sandbox Code Playgroud)
这实际上在桌面上创建了一个文件,但没有给它任何内容.
如果我只是不管道到Out-File最后一行,它工作正常,并将反转的文件直接打印到PowerShell窗口.
我已尝试使用和不使用单引号,以及使用-FilePath选项.没有什么工作.有什么想法吗?
我知道这是相对简单的任务,但我是 PowerShell 新手,在这种情况下互联网搜索并没有真正帮助我:(
我有以下字符串 -
server1.us.domain.com
server2.domain.com
Run Code Online (Sandbox Code Playgroud)
我想从这些中提取 dns 后缀,即。
us.domain.com
domain.com
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法。到目前为止我能到达的最接近的是
$a = 'us.domain.com'
PS C:\> $a.Split('.')
us
domain
com
Run Code Online (Sandbox Code Playgroud) 有没有办法在不使用 Powershell 安装 Excel 的情况下将 .xls 转换为 .csv?
我无法在特定机器上访问 Excel,因此在尝试时出现错误:
New-Object -ComObject excel.application
Run Code Online (Sandbox Code Playgroud)
新对象:检索具有 CLSID {00000000-0000-0000-0000-000000000000} 的组件的 COM 类工厂由于以下错误而失败:80040154 类未注册(来自 HRESULT 的异常:0x80040154)。
我正在尝试在目录c:\bats\中搜索包含unc路径的批处理文件\\server\public
命令:
Get-ChildItem -path c:\bats\ -recurse | Select-string -pattern "\\server\public"
Run Code Online (Sandbox Code Playgroud)
我收到与字符串相关的错误\\server\public:
Select-string : The string \\server\public is not a valid regular
expression: parsing "\\server\public" - Malformed \p{X} character
escape. At line:1 char:91
+ ... ts" -recurse | Select-string -pattern \\server\public
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-String], ArgumentException
+ FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand
Run Code Online (Sandbox Code Playgroud)
我一直在使用各种转义如试图"\server\public"或"'\server\public'"但我总是收到同样的错误.
我正在编写一个简单的脚本,该脚本应该根据一年中的哪一周将联系人添加到通讯组。我的错误是我的脚本可以添加,objectclass:User但是当我尝试使用联系人 GUID 时,脚本给了我这个错误:
Add-ADGroupMember : Cannot find an object with identity: '123dd2345-12f0-542b-c3e6-5774bac431aa' under: 'DC=MY,DC=DOMAIN'.
At line:1 char:25
+ get-adgroup $ADGroup | Add-ADGroupMember -members $zvar.ObjectGUID
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (123dd2345-12f0-542b-c3e6-5774bac431aa:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
Run Code Online (Sandbox Code Playgroud)
我使用的脚本部分如下所示:
$zvar = get-adobject -filter {displayname -eq "Valentine, John (CELL)" } #this is my contact displayname that is put in a variable with necessary properties
get-adgroup "Dist - Support group" | Add-ADGroupMember -members $zvar.ObjectGUID #this is my Distribution group, whatever the …Run Code Online (Sandbox Code Playgroud) powershell active-directory active-directory-group powershell-3.0
在web.config文件中httpGetEnabled,httpsGetEnabled如果它们不存在,我必须启用和属性.
$Path = "c:\web.config"
$XPath = "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior"
if ( Select-XML -Path $Path -Xpath $XPath ) {
"Path available"
$attributePath = $Xpath +="/serviceMetadata"
"Attribute path is $attributePath"
If (Get-XMLAttribute -Path $Path -Xpath $attributePath -attribute "httpGetEnabled" ) {
"httpGetEnabled is present"
}
ElseIf (Get-XMLAttribute -Path $Path -Xpath $attributePath -attribute "httpsGetEnabled") {
"httpsGetEnabled is present"
}
Else {
"Add both httpGetEnabled and httpsGetEnabled attribute with the value true and false accordingly"
$attributeset = @" httpGetEnabled="false" "@
New-Attribute -path $path …Run Code Online (Sandbox Code Playgroud) 我正在为我儿子写的剧本有问题.我的意图是提醒他记住他的家务.我刚刚开始做PowerShell,我真的非常喜欢它.我已经买了几本书,还经历了很多其他的帖子.
到目前为止我所得到的是,似乎评估不能正常使用-or (或者我可能会这么做?)
## REMINDERS TO DO CHORES ##
$sun = "Sunday"
$mon = "Monday"
$tue = "Tuesday"
$wed = "Wednesday"
$thu = "Thursday"
$fri = "Friday"
$sat = "Saturday"
$today = (get-date).DayOfWeek
$choreVac = "Vacuum the rooms and stairs"
$choreBath = "Clean the Bathroom Including emptying the garbage"
$choreOther = "No Chores Today -- But keep dishes done up"
if($today -eq $mon -or $wed -or $fri) {
msg /time:2500 * "Today is a Chore Day: your job is …Run Code Online (Sandbox Code Playgroud) 我有一个Windows nano服务器,并尝试设置代理设置.nano服务器仅在命令模式下没有GUI.我在PowerShell中运行
netsh winhttp set proxy proxy-server="ipadress:8080"
Run Code Online (Sandbox Code Playgroud)
然后我有
ping www.google.de
Run Code Online (Sandbox Code Playgroud)
并且显示了谷歌的IP地址,因此存在一些连接.但是当我试着奔跑的时候
wget www.google.de
Run Code Online (Sandbox Code Playgroud)
我明白了
"Unable to connect to the remote server"
Run Code Online (Sandbox Code Playgroud)
然后我在PowerShell环境中设置了代理
set http_proxy="ipadress:8080" and https_proxy...
Run Code Online (Sandbox Code Playgroud)
但同样的问题.当我使用代理设置直接调用wget时,它可以工作:
wget 'http://www.google.de' -Proxy http://ipadress:8080
Run Code Online (Sandbox Code Playgroud)
如何才能wget使用全局代理设置?或者设置没有正确设置?或者需要我安装一些Windows功能,它是否正常工作?
我wget用来测试连接以后任何程序的Web请求应该工作.
我不知道如何用通常的嫌疑人这样做,即Where-Object或Select-Object.
假设我想在PSCustomObject中找到字符串"needle" $Object,并且该对象可以有几个Note属性,例如$Object.Haystack1,$Object.Haystack2等等.
在我的情况下,注释属性的数量是已知的并且是固定的,但是当你不知道对象有多少属性时,我想知道如何处理更难的情况.
我和Select/ Where-Object和操作员一起探索,-in但没有设法制作一个简单,优雅的衬里来完成这项工作.