标签: powershell

如何使用 Powershell 授予远程启动/停止服务的权限?

我们有一个 PowerShell 脚本,可以在另一台计算机上重新启动服务。当我们使用 PowerShell 的内置服务控制 cmdlet 时,像这样:

$svc = Get-Service -Name MyService -ComputerName myservicehostname
Stop-Service -InputObject $svc
Start-Service -InputObject $svc
Run Code Online (Sandbox Code Playgroud)

我们得到这个错误:

停止服务:无法在计算机“myservicehostname”上打开 MyService 服务。

但是,当我们使用 sc.exe 时,像这样:

C:\Windows\System32\sc \\myservicehostname stop MyService
C:\Windows\System32\sc \\myservicehostname start MyService
Run Code Online (Sandbox Code Playgroud)

启动和停止成功。

重新启动的用户不是管理员。我们使用 subinacl 授予用户启动/停止和查询服务的权限:

subinacl.exe /service MyService /GRANT=MyServiceControlUser=STO
Run Code Online (Sandbox Code Playgroud)

为什么 PowerShell 不能停止我的服务但sc.exe可以?

powershell windows-service

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

Powershell 参数

我的脚本中有一个 Param 块

Param (
    [Parameter(Mandatory=$True)]
    [string]$FileLocation,

    [Parameter(Mandatory=$True)]
    [string]$password = Read-Host "Type the password you would like to set all the users to" -assecurestring
)
Run Code Online (Sandbox Code Playgroud)

我可以在必填的参数字段中使用 Read-Host CmdLet 吗?如果不是,我能做些什么来确保我采用正确类型的变量类型,以便我可以将它传递给用户创建过程?

scripting powershell shell-scripting

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

如何使用 PowerShell 在 Active Directory 中查找孤立的计算机对象?

如何使用 PowerShell 查找我的 Active Directory 域中已不活动 x 天的所有计算机帐户?

请注意,我确实知道如何做到这一点。这是一个自我回答的问题,只是为了获得知识。如果其他人有更好的方法,请随时发布!

powershell active-directory

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

Powershell 使用 CNAME 远程处理到服务器,而不是计算机名称

我试图让 powershell 远程处理使用它的 CNAME 而不是计算机名称与服务器一起工作。本地和远程机器都在同一个网络和域中。

我启用了 powershell 远程处理,并且它与计算机名称一起工作正常。我已将 TrustedHosts 设置为 CNAME 并为 CNAME 添加了 SPN,但是当我发出时,Enter-PSSession CNAME我得到以下信息:

Enter-PSSession : Connecting to remote server CNAME failed with the following
error message : WinRM cannot process the request. The following error
occurred while using Kerberos authentication: Cannot find the computer CNAME.
Verify that the computer exists on the network and that the name
provided is spelled correctly. For more information, see the
about_Remote_Troubleshooting Help topic.
Run Code Online (Sandbox Code Playgroud)

执行setspn -l COMPUTERNAME给了我这个:

Registered …
Run Code Online (Sandbox Code Playgroud)

powershell windows-server-2008

9
推荐指数
1
解决办法
7020
查看次数

未激活通过 appcmd 更改 IIS 上的物理路径

我们在 IIS 7.5 上遇到了一个问题,我们有一个简单的部署系统,它包含以下内容:

创建一个新的 webroot 的 zip 文件,包含三个文件夹:

Api
Site
Manager
Run Code Online (Sandbox Code Playgroud)

它被解压到一个新文件夹中(假设我们称之为“SITE_REV1”),并包含一个调用以下内容的脚本(每个 webroot 一个):

C:\Windows\system32\inetsrv\appcmd set vdir "www.site.com/" -physicalPath:"SITE_REV1\Site"

这通常工作,在 9/10 倍。在某些情况下,webroot 似乎已正确更新(如果我检查 IIS 管理器中的基本设置,路径看起来是正确的),但有问题的运行站点实际上指向旧位置。我们设法“修复它”的唯一方法是运行 IIS 重置。仅回收有问题的应用程序池是不够的。

有时似乎甚至有必要重新启动,但我不能 100% 确定这是准确的(并非总是我自己在解决问题)。

我使用 Powershell 和 Web-Administration 模块重写了脚本,希望 appcmd 中存在故障,但发生了同样的问题。

Set-ItemProperty "IIS:\Sites\www.site.com" -Name physicalPath -Value "SITE_REV1\Site"

有没有人经历过这样的事情?有没有人知道发生了什么,我可以尝试做些什么来防止这个问题?执行 IIS 重置对我们来说并不是一个好的选择,因为每次我们尝试在单个站点上部署更改时,这都会影响服务器上的所有站点。

编辑:我们已经确定在 IIS 管理器中启动/停止站点(而不是应用程序池)可以解决错误的物理路径,但是如果我使用 appcmd 停止站点,更改物理路径,然后启动它,我仍然遭受同样的问题。我在一片空白...

scripting powershell deployment iis-7.5 appcmd

9
推荐指数
1
解决办法
6060
查看次数

使用 PowerShell 检测 AD 站点选项

如何使用 PowerShell 查找 AD 站点选项,例如+IS_INTER_SITE_AUTO_TOPOLOGY_DISABLED在 PowerShell 中?我一直在玩以下命令,但无法让它吐出任何有用的东西。

Get-ADObject -Filter 'objectClass -eq "site"' -Searchbase (Get-ADRootDSE).ConfigurationNamingContext` -Properties options
Run Code Online (Sandbox Code Playgroud)

powershell active-directory

9
推荐指数
1
解决办法
4265
查看次数

使用 Powershell 编辑/设置 Mime 类型?

所以我想知道是否有一种特定的方法可以使用 powershell 在 IIS 7.5 中更改/编辑 mime 类型?

到目前为止,我所遇到的解决方案,以添加MIME类型,如: http://sclarson.blogspot.co.at/2011/08/adding-mime-type-to-iis-vis-powershell.html HTTP://论坛。 iis.net/t/1192371.aspx 和其他一些有书籍被引用但这些书籍不包含任何信息,我查了一下。

似乎使用了Add-WebConfigurationPropertyis used,但是当我将此方法用于现有的 mimetype 时,我将收到与重复项条目相关的错误(因为它已经在那里),因此此方法不在窗口中。

我试图改用 ,Set-WebConfigurationProperty但这导致它删除了所有现有的 mimetype 条目并只留下其中一个。

那么有没有人有建议或者可能知道这样做的正确方法?我还考虑过先删除条目然后添加它,唯一的问题是您需要知道 mimetype fileExtension 和 mimeTypes(即文本/XML 等)。所以我假设我需要首先使用该getwebconfigproperty方法获取两个属性,然后通过将其中的值解析到函数中来删除它delete-webpropconfig,然后添加它......但是仅仅通过三个步骤来设置一个 mimetype 似乎过度了..

据我所知,appcmd.exe set config "values and params here" 方法几乎可以直接执行.. 然而,这个问题是不幸的是我们不能使用它。是的,我知道我可以让 powershell 执行 appcmd 命令,但不幸的是这不是一个可行的选择。我曾尝试谷歌搜索和阅读书籍以寻求解决方案,但到目前为止还没有想出答案

1) 该set-webconfigurationproperty方法删除所有其他 mimetype 条目

2)add-webconfigurationproperty不允许我执行它并抛出“错误重复”消息

3) 在 1 行中使用 powershell 执行此操作的可行解决方案。

iis powershell

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

如何查找本地用户帐户的创建日期?

我想获取本地用户帐户的创建日期(如果重要,请使用 Win 7)。我查看了以下 WMI 对象(当然还有谷歌):

Win32_UserAccount Win32_NetworkLoginProfile

返回的对象NetworkLoginProfile具有上次登录时间,但没有创建日期。检查Date Created他们的配置文件文件夹的属性只会给出该文件夹的创建日期,不一定是帐户本身。

windows powershell wmi user-accounts

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

复制项目文件系统提供程序错误

Copy-item cmdlet 没有按预期工作,我不明白为什么。这是我的代码:

$Source = "C:\folder1"
$Destination = "\\172.22.0.115\c$\folder2\"
$Password  = ConvertTo-SecureString -AsPlainText -Force -String "MyPassword"
$User = "Domain\Administrator"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password

Copy-Item $Source -Destination $Destination -Credential $credentials 
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform 
the operation again without specifying credentials.
At C:\Sans titre2.ps1:7 char:1
+ Copy-Item $Source -Destination $Destination -Credential $credentials
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

Start-BitsTransfer -Source $source -Destination $Destination …
Run Code Online (Sandbox Code Playgroud)

scripting windows powershell windows-server-2012 windows-server-2012-r2

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

ESXcli 命令超时 30 分钟

我正在编写一个 powercli 脚本来循环遍历我们环境中的每个数据存储并回收精简配置的 LUN 上的空间。环境中的所有 LUN 都是 Dell Equal Logic。

vCenter 版本: 5.5.0_1750787

ESXi 版本: Dell 5.5-1331820(A01)

我遇到的问题是,在脚本中执行 unmap 命令后正好 30 分钟,我收到以下错误:

从 PowerCLI:

Message:  An error occurred while communicating with the remote host:
InnerText:
At \\SOMEIP\Share\folder\vmware_unmap_datastore.ps1:96 char:9
+        $RETVAL = $ESXCLI.storage.vmfs.unmap(200, $ds.Name, $null)
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CatagoryInfo          : OperationsStopped: (:) [], HostCommunication
    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.HostCommunication
Run Code Online (Sandbox Code Playgroud)

来自:ESXi 主机 vpxa.log:

==> vpxa.log <==
2014-06-30T14:39:51.907Z [7265EB70 error 'vpxavpxaVmomi' opID=60e7b2a5-11]
[VpxaClientAdapter::InvokeCommon] Got exception while invoking executeSoap
on vmodl.reflect.ManagedMethodExecuter:ha-managed-method-executer:
'Operation timed out', backtrace: …
Run Code Online (Sandbox Code Playgroud)

powershell timeout vmware-esxi powercli dell-equallogic

9
推荐指数
1
解决办法
8089
查看次数