标签: powershell

将 Powershell 转换为嵌套的 Json

所以我已经尝试解决这个问题几天了,但似乎找不到任何有效的方法,所以我希望你能帮助我或指出正确的方向。

我正在创建一个在 Active Directory 中创建服务帐户的 powershell 脚本,在脚本的最后是用户名和密码,然后存储在 Enterprise Password Vault 中。REST Api 用于在此 Vault 中存储凭据。我在将我的 powershell 转换为嵌套的 Json 时遇到了一个小问题,没有嵌套它工作正常......

我需要得到的 JSON 输出是这样的:

{
"account":{
    "safe":"Test_safe",
    "platformID":"WindowsDomainAccount",
    "address":"ad.local",
    "password":"password",
    "username":"test",
    "disableAutoMgmt":"true",
    "disableAutoMgmtReason":"test",
    "properties":
    [
    {"Key":"Title", "Value":"Test Account"},
    {"Key":"Description", "Value":"REQ0000001"},
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Powershell,不包括工作正常的“属性”嵌套。

   $hash = [ordered]@{
        account = [ordered]@{
            safe="ServiceAccounts"; 
            platformID="WindowsDomainAccount";
            address="ad.local";
            password="Password1";
            username="svc-test";
            disableAutoMgmt="true";
            disableAutoMgmtReason="testing";
        }
   }

$json = $hash | ConvertTo-Json

$invoke = Invoke-Webrequest -Uri $url -Method "POST" -Body $json -ContentType "application/json" -Header @{"Authorization" = $header} -ErrorAction Stop …
Run Code Online (Sandbox Code Playgroud)

powershell json

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

Out-File:不接受输出文件路径

仍在学习并且我很难尝试将信息输出到文件:不接受输出文件路径。

我的位置是PS Cert:\localmachine,这里是整个命令:

$cert = Get-ChildItem -Path cert: -Recurse | where { $_.notafter -le (get-date).AddDays(75) -AND $_.notafter -gt (get-date)} | select notafter, issuer, thumbprint, subject | sort-object notafter
$cert | Out-File -FilePath \\ad.dcpds.cpms.osd.mil\WinAdm\Logs\Expiring_Certificates\$hostname.log
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息是:

Out-File : Cannot open file because the current provider (Microsoft.PowerShell.Security\Certificate) cannot open a file.

powershell

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

如何从 C# 中的 Active Directory 用户获取所有属性

我一直在寻找一种使用 C# 代码的解决方案,该解决方案可以查询 Active Directory 用户已注册的所有属性,无论它们是否具有 NULL 值。这些属性通过域服务器上 ADSI Edit 中用户属性中的“属性编辑器”选项卡可见。

ADSI 编辑中的 AD 用户属性

我需要动态检索这些属性,这意味着我可能无法通过 MSDN 上的 ADSI 文档可靠地获取这些属性名称,因为并非所有这些属性都可能是特定于用户对象的:https : //msdn.microsoft.com/en -us/library/ms675090(v=vs.85).aspx

这是我到目前为止所尝试的,但只得到了用户对象的一小部分属性:

  1. PS 命令Get-ADUser -Identity administrator -Properties:这检索了很大一部分属性,但不是几乎所有属性,我不知道在此命令期间调用了哪些 .NET 类和方法,因为TypeName = Microsoft.ActiveDirectory.Management.ADUser在 .NET 框架中不存在。如何在 PS 中查看 .NET 中使用的特定方法?

  2. C# 调用此方法:

    public bool GetUserAttributes(out List<string> userAttributes, string userName)
    {
        userAttributes = new List<string>();
        var valueReturn = false;
    
        try
        {
            const string pathNameDomain = "LDAP://test.local";
    
            var directoryEntry = new DirectoryEntry(pathNameDomain);
    
            var directorySearcher = new DirectorySearcher(directoryEntry)
            { …
    Run Code Online (Sandbox Code Playgroud)

c# powershell attributes ldap active-directory

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

powershell '&lt;' 运算符保留供将来在 Java 中使用

我已经阅读了很多关于这个问题的答案,但没有发现关于两个文件之间的比较,实际上这是Algorithms based on BinarySearch一书的样本,这里是源代码

import java.util.Arrays;
import edu.princeton.cs.algs4.*;

public class prac1_1_23{

public static boolean BinaryLookup(int key, int[] arr) {
    int low = 0;
    int high = arr.length - 1;
    while(low <= high) {
        int mid = low + ((high - low) >> 1);
        if(key < arr[mid])
            high = mid - 1;
        else if(key > arr[mid])
            low = mid + 1;
        else
            return true;
    }
    return false;
}

public static void main(String[] args) {
    char symbol = '-';
    int[] …
Run Code Online (Sandbox Code Playgroud)

java algorithm powershell

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

如何编写多个对象链方法调用以在多行代码中继续调用?

我遇到了很多关于如何跨多行继续 powershell 脚本代码的示例,例如,调用使用“反勾号”(`)传递多个参数的方法:

myMethod $arg1 `
   $arg2 `
   $arg3 `
   ...etc.
Run Code Online (Sandbox Code Playgroud)

但是我想做的是继续跨多行链接对象方法链,例如,我想将其分解为多行代码:

$ps = [PowerShell]::Create()
[ref]$e = New-Object System.Management.Automation.Runspaces.PSSnapInException
$ps.Runspace.RunspaceConfiguration.AddPSSnapIn( "SqlServerCmdletSnapin100", $e ) | Out-Null
# This is ridiculous; how to I break the below code across multiple lines???
$ps.AddCommand( "Invoke-Sqlcmd").AddParameter( "InputFile", $PreCompareInfoObject.PreCompareBatchScript).AddParameter("serverinstance", $DBConnectInfoObject.DBDestServer).AddParameter("database", $DBConnectInfoObject.DBDestDB).AddParameter( "username", $DBConnectInfoObject.DBDestUserName).AddParameter("password",  $DBConnectInfoObject.DBDestPassword).AddParameter( "variable",  $variable).AddParameter("outputsqlerrors",  $true).AddParameter("abortonerror").AddParameter("Verbose") | out-file -filepath $PreCompareInfoObject.PreCompareOutputReport
$ps.Invoke()
Run Code Online (Sandbox Code Playgroud)

我可以这样做,但我不想这样做,并且由于某种原因,输出比我想要的更冗长:

...
$ps.AddCommand( "Invoke-Sqlcmd")
$ps.AddParameter( "InputFile", $PreCompareInfoObject.PreCompareBatchScript)
$ps.AddParameter("serverinstance", $DBConnectInfoObject.DBDestServer)
$ps.AddParameter("database", $DBConnectInfoObject.DBDestDB)
$ps.AddParameter( "username", $DBConnectInfoObject.DBDestUserName)
$ps.AddParameter("password",  $DBConnectInfoObject.DBDestPassword)
$ps.AddParameter( "variable",  $variable)
$ps.AddParameter("outputsqlerrors",  $true)
$ps.AddParameter("abortonerror")
$ps.AddParameter("Verbose") | …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

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

任务计划程序 PowerShell 启动进程未运行

我在 Windows 10 上有这个简单的脚本,它在执行时工作正常,但从任务计划程序运行时无法启动记事本。Stop-Process 完美运行,Start-Process 不运行。当我按需运行它时,它会关闭记事本,然后在不打开记事本的情况下继续运行,该任务也不会关闭。

Stop-Process -processname notepad
Start-Process "C:\Windows\system32\notepad.exe"
Run Code Online (Sandbox Code Playgroud)

这是它被配置为运行的方式。 在此处输入图片说明 在此处输入图片说明 我尝试过的事情,但仍然不起作用。

  1. 首先,我在管理员帐户下运行。
  2. 在任务调度程序中,检查以最高权限运行。
  3. 我曾尝试-ExecutionPolicy Bypass和-ExecutionPolicy RemoteSigned
  4. 根据安全策略已授予我的用户Logon as batch工作权限
  5. 关闭 UAC

powershell scheduled-tasks start-process

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

使用 PowerShell 添加 .inf 驱动程序,可能吗?

我们是否能够使用 PowerShell cmdlet 通过其 .inf 文件等安装驱动程序?当谷歌搜索时,我发现 Add-WindowsDriver 但我认为这是一个离线 Windows 映像。这是否意味着当前未在操作系统上使用的映像?如果没有,请教我如何写参数。谢谢!

powershell drivers

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

String.Split() 的奇怪结果

为什么以下结果会产生一个包含 7 个元素和 5 个空白的数组?我希望只有 2 个元素。5个空白元素来自哪里?

$a = 'OU=RAH,OU=RAC'

$b = $a.Split('OU=')
$b.Count 
$b

<#

Outputs:

7



RAH,


RAC

#>
Run Code Online (Sandbox Code Playgroud)

powershell

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

PowerShell 顺序敏感比较对象差异

是否可以在 PowerShell 中使用Compare-Objectakadiff对数组中的项目进行顺序比较?如果没有,建议一个解决方法。

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,5,4)
if (Compare-Object $a1 $b1) {
    Write-Host 'Wrong order '
}
else {
    Write-Host 'OK' 
}
Run Code Online (Sandbox Code Playgroud)

powershell

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

需要“包”参数来执行相对导入

我不确定是什么导致了这个错误,我想尝试从 Windows PowerShell 在 django 上运行,但我遇到了这个错误和问题。

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03D366A8>
Traceback (most recent call last):
  File "D:\Python\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "D:\Python\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "D:\Python\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\Python\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\Python\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\Python\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "D:\Python\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "D:\Python\lib\site-packages\django\utils\functional.py", …
Run Code Online (Sandbox Code Playgroud)

python django powershell

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