小编Mat*_*att的帖子

以管理员身份运行 PS,但收到错误 - 该操作需要提升

由于各种新的安全策略,我们需要能够作为分配的 uid 运行各种任务。为了帮助实现这一点,开发了一个通用菜单系统。脚本中的函数之一是:

$credential = user_credential
$cmd = "C:\windows\system32\mmc.exe" 
$args = "C:\Windows\System32\compmgmt.msc"
Start-Process -FilePath $cmd -ArgumentList $args  -Credential $credential
Run Code Online (Sandbox Code Playgroud)

收到错误:

Start-Process : This command cannot be run due to the error: The requested operation requires elevation.
t C:\webeng\webeng.ps1:131 char:5
     Start-Process -FilePath $cmd -ArgumentList $args  -Credential $credential
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
   + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Run Code Online (Sandbox Code Playgroud)

该错误似乎表明 PS 脚本需要提升权限,但我已经以管理员身份运行。

我在这里可能会错过什么?

powershell credentials invalidoperationexception

6
推荐指数
1
解决办法
2万
查看次数

使用PowerShell在网页中查找特定句子

我需要使用powershell通过whois解析IP地址.我的公司过滤端口43和WHOIS查询,因此我必须使用的解决方法是让powershell使用https://who.is等网站,读取http流并查找与IP地址匹配的组织名称.

到目前为止,我已经设法将网页读入PowerShell(例如,在yahoo.com上使用WHOIS),这是https://who.is/whois-ip/ip-address/206.190.36.45

所以这是我的片段:

$url=Invoke-WebRequest https://who.is/whois-ip/ip-address/206.190.36.45
Run Code Online (Sandbox Code Playgroud)

现在,如果我这样做:

$url.gettype()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    HtmlWebResponseObject                    Microsoft.PowerShell.Commands.WebResponseObject
Run Code Online (Sandbox Code Playgroud)

我看到这个对象有几个属性:

Name              MemberType Definition
----              ---------- ----------
Equals            Method     bool Equals(System.Object obj)
GetHashCode       Method     int GetHashCode()
GetType           Method     type GetType()
ToString          Method     string ToString()
AllElements       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}
BaseResponse      Property   System.Net.WebResponse BaseResponse {get;set;}
Content           Property   string Content {get;}
Forms             Property   Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}
Headers           Property   System.Collections.Generic.Dictionary[string,string] Headers {get;}
Images            Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields …
Run Code Online (Sandbox Code Playgroud)

string powershell

6
推荐指数
3
解决办法
2万
查看次数

Azure PowerShell无法连接

我按照MS说明下载并安装了Azure Powershell:https: //azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

使用Windows Powershell ISE我运行命令:

登录-AzureRmAccount

返回错误:

Login-AzureRmAccount : The 'Login-AzureRmAccount' command was found in the module 'AzureRM.Profile', but the  module could not be loaded. For more information, run 'Import-Module AzureRM.Profile'. At line:1 char:1
+ Login-AzureRmAccount
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Login-AzureRmAccount:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
Run Code Online (Sandbox Code Playgroud)

导入模块AzureRM.Profile

返回此错误:

Import-Module : File C:\Program Files (x86)\Microsoft 
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\CheckVersions.ps1 cannot be loaded 
because running scripts is disabled on this system. For more information, see about_Execution_Policies at 
http://go.microsoft.com/fwlink/?LinkID=135170.
At …
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-powershell

6
推荐指数
1
解决办法
1万
查看次数

Security.SecureString参数不接受字符串

将字符串作为参数传递给此参数时:

[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Security.SecureString]$password=$(Throw "Password required.")
Run Code Online (Sandbox Code Playgroud)

使用-password参数时出现此错误.

无法将System.String类型转换为System.Security.SecureString类型

如果我没有传递-password参数,则会显示提示并接受输入.

passwords powershell parameter-passing

6
推荐指数
1
解决办法
9718
查看次数

使用括号进行多个逻辑运算

考虑以下语句:

Get-ChildItem -Recurse *.vbs | Where-Object{$_.name -like "d*" -and $_.name -like "*s"}
Get-ChildItem -Recurse *.vbs | Where-Object{($_.name -like "d*") -and ($_.name -like "*s")}
Run Code Online (Sandbox Code Playgroud)

它们将产生完全相同的输出。我一直认为第一个语句会失败,因为条件不在括号中。

查看about_Logical_Operators的示例-and-or显示这些括号。

(1 -eq 1) -or (1 -eq 2)
Run Code Online (Sandbox Code Playgroud)

但是随后有关使用 Where-Object Cmdlet的技术网文章显示了此示例

$_.handles -gt 200 -and $_.name -eq "svchost"
Run Code Online (Sandbox Code Playgroud)

有一段时间我一直在告诉人们使用括号,因为“否则它就行不通”。区别是否纯粹是表面上的,最终无关紧要,我只是在推动我的个人喜好?

powershell parentheses logical-operators

5
推荐指数
1
解决办法
750
查看次数

Nagios - 无法打开命令文件/usr/local/nagios/var/rw/nagios.cmd'进行更新

我试图通过Nagios网络界面禁用对服务的主动检查,但我无法做到.Nagios实例在Centos 5发行版上运行

每次我尝试停止服务时都会收到以下消息:无法打开命令文件'/usr/local/nagios/var/rw/nagios.cmd'进行更新!

虽然我尝试了在网上找到的几个提案:

usermod -a -G nagios apache
chmod 666 /usr/local/nagios/var/rw/nagios.cmd
chown nagios.nagcmd /usr/local/nagios/var/rw
chmod u+rwx /usr/local/nagios/var/rw
chmod g+rwx /usr/local/nagios/var/rw  
chmod g+s /usr/local/nagios/var/rw 
Run Code Online (Sandbox Code Playgroud)

当然,每次重启nagios服务.

谢谢你的帮助 !干杯

monitoring nagios

5
推荐指数
3
解决办法
3万
查看次数

使用ConvertTo-HTML将URL列表转换为可单击的HTML链接

我下面的函数将创建一个HTML表,但是我希望表中的值具有超链接。

Function MyFunction{

clear-host
$item=$null
$hash = $null #hashTable

$hash =@{"Google.com" = "www.google.com"; "Yahoo" = "www.yahoo.com";"My Directory" ="C:\Users\Public\Favorites" ;"MSN" = "www.msn.com"}

ForEach($item in @($hash.KEYS.GetEnumerator())){
$hash | Sort-Object -Property name
}

$hash.GetEnumerator() | sort -Property Name |Select-Object Name, Value | ConvertTo-HTML | Out-File .\Test.html

}

MyFunction
Run Code Online (Sandbox Code Playgroud)

powershell

5
推荐指数
1
解决办法
6688
查看次数

Powershell - JSON 文件到表

我正在尝试进入 Powershell 并尝试将一个简单的 JSON 文件转换为表格格式。

json 文件如下所示:

{ "Members" : [
{ "Id" : 1,
    "Details" : [ { "FirstName" : "Adam" }, { "LastName" : "Ant" } ] },
{ "Id" : 2,
    "Details" : [ { "FirstName" : "Ben" }, { "LastName" : "Boggs" } ] },
{ "Id" : 3,
    "Details" : [ { "FirstName" : "Carl"} , { "LastName" : "Cox" } ] }
]
}
Run Code Online (Sandbox Code Playgroud)

Powershell 表达式:

$jString.Members.details | Select-Object -Property Id, FirstName, LastName
Run Code Online (Sandbox Code Playgroud)

到目前为止的输出(我得到的最好).. …

powershell json powershell-3.0

5
推荐指数
1
解决办法
1万
查看次数

Get-ADGroup - 组名,ManagedBy名称和电子邮件

我想在类似于此的PowerShell查询中获取组名,按名称管理和通过电子邮件管理.

Get-ADGroup -filter {Name -like "*Admins" }
Run Code Online (Sandbox Code Playgroud)

输出看起来类似于:

Group Name | Managed By Name | Managed By Email
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是加入Get-ADGroupGet-ADUser.在SQL中,这种"连接"会发生get-adgroup.managedby = get-aduser.distinguishedname.我知道这不是它在Powershell中的工作原理,只是想我会抛出一个我想要做的事情的例子.

任何帮助都会受到欢迎和赞赏.

powershell

5
推荐指数
2
解决办法
2万
查看次数

如何替换文件夹中 Word 文档中所有出现的字符串

我已经设法找到并编辑每个单词文件。有了这个代码:

$objWord = New-Object -comobject Word.Application  
$objWord.Visible = $false

$objDoc = $objWord.Documents.Open("C:\users\stefan\test\New Microsoft Word Document.docx") 
$objSelection = $objWord.Selection 

$FindText = "that" 
$MatchCase = $False 
$MatchWholeWord = $true
$MatchWildcards = $False 
$MatchSoundsLike = $False 
$MatchAllWordForms = $False 
$Forward = $True 
$Wrap = $wdFindContinue 
$Format = $False 
$wdReplaceNone = 0 
$ReplaceWith = "this" 
$wdFindContinue = 1 

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
$Wrap,$Format,$ReplaceWith) 
$objDoc.Save()
$objWord.Quit()
Run Code Online (Sandbox Code Playgroud)

但我想对整个文件夹执行此操作。我尝试插入这样的内容:

$objWord = New-Object -comobject Word.Application  
$objWord.Visible = $false

 $list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
 foreach($item in $list){ …
Run Code Online (Sandbox Code Playgroud)

com powershell ms-word

5
推荐指数
1
解决办法
1万
查看次数