标签: powershell

检查用户是否具有读/写权限

我给了用户域\名称,密码和路径,我想显示给定用户是否在给定目录中具有写入和/或读取访问权限.

大概的概念:

function Has-Read-Access {
    param(
        [Parameter(Mandatory=$true)][string]$userLogin
        [Parameter(Mandatory=$true)][string]$userPwd
        [Parameter(Mandatory=$true)][string]$directory
    )
    $hasReadAccess = 0
    #insert magic here
    return $hasReadAccess
}

Has-Read-Access -userLogin 'DOMAIN\user' -userPwd 'Passw0rd' -directory 'C:\FolderName\'
Run Code Online (Sandbox Code Playgroud)

我尝试过powershell.exe不同的用户运行:

Start-Process powershell.exe -Credential "TestDomain\Me"
Run Code Online (Sandbox Code Playgroud)

但这似乎是一种错误的做法.

windows powershell file-permissions

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

如何从Azure Resource Manager Runbook登录?

使用新的Azure门户,我试图添加将启动特定VM的Powershell Runbook。这不是可以从我的PC在Powershell中运行的东西,而是作为ARM作业运行的。我似乎找不到成功登录的方法。

如果在Powershell中从桌面运行,我可以调用Login-AzureRmAccount,它将在运行任何其他步骤之前启动登录对话框。从我在网络上阅读的内容来看,我似乎需要做的是向我的自动化帐户添加凭据,对其进行检索,然后调用相同的Login方法。我现在已经完成了,但是仍然无法登录。

Import-Module AzureRM.Compute
$AutomationCredentialAssetName = "automation"
$Cred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName
Write-Output $Cred
Login-AzureRmAccount -Credential $Cred
Start-AzureRmVm -Name 'myvmname' -ResourceGroupName 'myresourcegroupname'
Run Code Online (Sandbox Code Playgroud)

凭证已正确检索(将其写入输出),但对Login-AzureRmAccount的调用失败,并显示以下信息:

Login-AzureRmAccount:未知用户类型:未知用户类型在线:10 char:1 + Login-AzureRmAccount -Credential $ Cred + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + AddAzureRMAccountCommand

如果我不尝试首先登录,则会收到一条消息,告诉我先致电Login-AzureRmAccount。

如何从运行手册中进行身份验证,以便可以运行自动化任务?我究竟做错了什么?

powershell login azure runbook

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

powershell中的这些对象是什么(1GB,11GB,...)

在Powershell中,你有一些数字的魔术后缀.也就是说,你可以前缀任意数量的MB,GB,TB,和一样,他们会以字节为单位返回的金额.

例;

PS C:\> 1TB
1099511627776

PS C:\> 123KB
125952
Run Code Online (Sandbox Code Playgroud)

这个黑魔法究竟是什么?它们显示为类型INT;

PS C:\> (123GB).getType() | ft -AutoSize

IsPublic IsSerial Name  BaseType        
-------- -------- ----  --------        
True     True     Int64 System.ValueType
Run Code Online (Sandbox Code Playgroud)

使用与普通INT相同的基本类型

PS C:\> ([int]123).getType() | ft -AutoSize

IsPublic IsSerial Name  BaseType        
-------- -------- ----  --------        
True     True     Int32 System.ValueType
Run Code Online (Sandbox Code Playgroud)

所以问题仍然存在;

  • 这些"事物"究竟是什么?
  • 我怎样才能创建自己的.
  • 在哪里/如何获得我可以使用的其他魔术前缀的列表?
  • 他们会是正确的吗?(ref kibibyte vs kilobyte(https://en.wikipedia.org/wiki/Kibibyte))

powershell

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

解释从PowerShell闭包调用的函数的范围

以下PowerShell代码显示从闭包调用的函数的意外作用域行为.你能解释一下这是"按设计"还是缺陷?

function runblock($block) {
    $x = 4
    & $block
}

function printx() {
    "  in printx: x=" + $x
}

"PSVersion $($PSVersionTable.PSVersion)"

$x = 1
$b = {"In block x=" + $x ; $x = 3 ; printx}
$x = 2
runblock $b

$x = 1
$b = {"In closure x=" + $x ; $x = 3 ; printx}.GetNewClosure()
$x = 2
runblock $b
Run Code Online (Sandbox Code Playgroud)

以上输出是

PSVersion 3.0
In block x=4
  in printx: x=3
In closure x=1
  in printx: x=4
Run Code Online (Sandbox Code Playgroud)

大部分输出对我有意义: …

powershell closures scriptblock

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

使用Invoke-Command时,Try&Catch无法正常工作

我的脚本中有一个奇怪的问题.我正在某些服务器上调用命令.如果我无法连接到服务器(因为它是脱机的或其他东西)我仍然想记录(服务器脱机> log.txt)

显然,当Try块中发生错误时,Catch不执行该块.

为了测试这个,我给ComputerName不存在的参数写了一个值.这是我的代码:

Try {
    Invoke-Command -ComputerName hui -ScriptBlock $sb
}
Catch {
    Write-Host "Hello"
}
Run Code Online (Sandbox Code Playgroud)

但是脚本从未发出"你好"

我得到的例外是PSRemotingTransportException.我也试着指定异常,也没用

当我在该Invoke-Command行设置断点时,它也会忽略catch块.为什么?

powershell try-catch

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

Azure资源模板部署问题

我为DocumentDB,Azure SQL Server,存储帐户,Azure Key Vault,Azure Batch,HDInsight Cluster创建了单独的ARM模板.

New-AzureRmResourceGroupDeployment当我在同一资源组中的循环中部署上述资源时使用powershell命令,我发现了一个奇怪的行为.在部署DocDB时,资源组中以前部署的所有资源都会消失(可能会自动删除).部署Azure SQL Server时的情况也是如此.

有人遇到过同样的问题吗?有修复吗?

powershell azure azure-resource-manager

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

如何使用Powershell从DnsServerResourceRecord获取真实的RecordData?

我正在使用PowerShell从Active Directory DNS服务器提取信息,但无法获取所需的数据。

具体来说,我正在尝试获取属于特定子网的主机的名称10.104.128.x

当我使用以下命令时:

Get-DnsServerResourceRecord-计算机名AD_DNS_SERVER-区域名104.10.in-addr.arpa -RRType Ptr | Where-Object {$ _。HostName -like'* .128'}`

我得到的输出看起来像这样:

主机名RecordType时间戳记TimeToLive RecordData                                        
-------- ---------- --------- ---------- ----------                                        
104.128 PTR 10/19/2015 3:00:0 ... 00:15:00 adl5c260a86ba79.XYZ.net。                          
11.128 PTR 2015年12月29日6:00:0 ... 00:15:00 adl3c970e8d7166.XYZ.net。                          
110.128 PTR 1/29/2012 11:00:0 ... 00:15:00 nroxitow7tst.ABC.com。                       
114.128 PTR 1/20/2012 7:00:00 AM 00:15:00 adl5c260a86c29e.ABC.com

我真正想要的是第一列(HostName),其中包含IP的最后两个八位位组;第五列(RecordData),其中包含IP分配给的主机的名称。

主机名是我真正想要/需要的数据。我在那里看到它!

因此,我使用该select命令缩减了管道系统中的输出。新命令如下所示:

Get-DnsServerResourceRecord-计算机名AD_DNS_SERVER-区域名104.10.in-addr.arpa -RRType Ptr | Where-Object {$ _。HostName -like'* .128'} | 选择主机名,记录数据

但是输出看起来像这样:

主机名RecordData                
-------- ----------               
104.128 DnsServerResourceRecordPtr
11.128 DnsServerResourceRecordPtr
110.128 …

dns powershell active-directory

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

Azure Power Shell - 如何更改网络子网

我正在尝试更改通过Azure中的资源管理器部署的现有VM.VM配置了错误的VirtualNetwork/Subnet.portal.azure.net无法更改此通道.

我已经设法通过运行:检索所需的网络适配器$adapter = Get-AzureRmNetworkInterface -Name xxxxx -ResourceGroupName xxxxx.

我可以看到当前配置的子网ID $adapter.IpConfigurations.Subnet,我已通过检索新的子网$net = Get-AzureRmVirtualNetwork -ResourceGroupName xxxxx -Name xxxxx.

我尝试改变$net.IpConfigurations.Subnet.Id = $net.Subnets.Id但我失败了:

资源xxxx引用的子网默认值不是同一个虚拟网络....

任何人都可以告诉我如何更改网络适配器使用的虚拟网络(如果它是使用资源管理器部署的)("经典"具有简单Set-Subnet命令).

powershell networking azure

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

组合2个PowerShell阵列

我正在试图找出如何组合2个阵列,如微软此处所述.

$Source = 'S:\Test\Out_Test\Departments'
$Array = Get-ChildItem $Source -Recurse

$Array.FullName | Measure-Object # 85
$Array.FullName + $Source | Measure-Object # 86
$Source + $Array.FullName | Measure-Object # 1
Run Code Online (Sandbox Code Playgroud)

以下只有1项要迭代:

$i = 0
foreach ($t in ($Source + $Array.FullName)) {
    $i++
    "Count is $i"
    $t
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果$Source$Array为空,它不再生成单独的对象并将它们全部粘在一起,如上例所示.有没有办法强制它成为单独的对象而不是一个连接的对象?

arrays powershell

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

在PowerShell SELECT中解析JSON

我正在尝试使用powershell命令列出所有Azure VM的大小.问题是该HardwareProfile属性返回一个JSON对象,我想解析它并只使用vmSize该对象的属性值.

所以我正在运行这个命令:

Get-AzureRmVM
Run Code Online (Sandbox Code Playgroud)

这给了我这个:

ResourceGroupName        : TESTRG
...
Name                     : ubuntu-server
...
HardwareProfile          : {
                             "vmSize": "Standard_DS2"
                           }
...
Run Code Online (Sandbox Code Playgroud)

注意HarwareProfile值中的JSON .

我想做的是:

Get-AzureRmVM | Select ResourceGroupName, Name, HardwareProfileText `
              | Out-Gridview -PassThru
Run Code Online (Sandbox Code Playgroud)

哪个有效 - 只是,我想摆脱HardwareProfileText中的JSON表示法.使用Format-Table看起来像这样:

ResourceGroupName Name          HardwareProfileText
----------------- ----          -------------------
TESTRG            ubuntu-server {...
Run Code Online (Sandbox Code Playgroud)

所以问题是:我怎样才能获得vmSize此表中的值?我可以潜入ConvertFrom-Json某个地方吗?

powershell json azure-powershell

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