标签: powershell

在 Powershell 中获取 appSettings 键值

我正在尝试从多台计算机上的配置文件中获取值。

这是每台计算机上的文件...我试图将 \gm107a\Updates\QC 与计算机名称一起放入 csv 文件中。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="updatelocation" value="\\gm107a\Updates\QC"/>
    <add key="filename" value="QualityControl.exe"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我发现这让我开始: https: //social.technet.microsoft.com/Forums/scriptcenter/en-US/268e03cb-1248-456f-bc89-ecc31cb0489b/powershell-script-to-read-xml-data-然后from-multiple-remote-computers在 powershell 中发现了这个获取 app.config 元素并尝试将其放在一起......记住我是全新的哈哈。

这是我到目前为止所拥有的:

function vert {
$hostnamenodes = get-content C:\Scripts\Computers.txt

foreach ($hostname in $hostnamenodes) {
[xml]$xml = Get-Content \\$hostname\C$\Drivers\Version.config
            #Add hostrecord to array
            $MasterArray = New-Object psobject -Property @{
            "ServerName" = $hostname
            "updatelocation" = $xml.SelectNodes('//add[@key="updatelocation"]/@value')[0].'#text'

}
write-output $masterarray
} }
vert | select servername,updatelocation | Export-Csv QA.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Unable to …
Run Code Online (Sandbox Code Playgroud)

xml powershell config

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

在不同的事件日志中创建以前删除的源不起作用

在测试 log4net EventLogAppender 期间,我已经与 Windows 事件日志进行了长时间的斗争,其行为不一致,我意识到 log4net 代码有效,但我的 Windows 事件日志是不合理的。

系统

  • 操作系统:Windows 8.1
  • C#:.Net 4.5,针对 x64 构建

创建错误案例

  • C#:在 TestLog1 中创建 Source1
  • C#:写入日志(有效)
  • Powershell:使用 powershell 删除日志
  • C# 在 TestLog2 中创建 Source1(不同日志)
  • C# 写入日志 <= 这表明 TestLog2 中没有日志条目!

我已经制作了完整的分步指南来重现问题:

1:在新日志中创建新源并写入

执行代码:

EventLog.CreateEventSource(source: "TestSource1", logName: "TestLog1");
EventLog myLog = new EventLog();
myLog.Source = "TestSource1";
myLog.WriteEntry("This is a message");
Run Code Online (Sandbox Code Playgroud)

使用 powershell 命令列出日志:

Get-EventLog -LogName *
Run Code Online (Sandbox Code Playgroud)

这将正确列出所有日志,包括包含 1 个日志条目的 TestLog1。我还可以使用以下 powershell 命令获取日志条目:

GetEventLog -LogName "TestLog1"
Run Code Online (Sandbox Code Playgroud)

这显示了日志中的单个日志消息。

2:使用powershell删除事件日志

Powershell命令:

Remove-EventLog -LogName "TestLog1"
Run Code Online (Sandbox Code Playgroud)

现在列出所有日志显示该日志实际上已被删除。再次使用 Powershell 命令: …

c# powershell eventlog-source

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

Powershell get-childitem 需要大量内存

我的问题与元过滤器上发布的问题几乎相同。

我需要使用 PowerShell 脚本来扫描大量文件。问题在于“Get-ChildItem”函数似乎坚持将整个文件夹和文件结构推入内存中。由于驱动器在 30,000 多个文件夹中包含超过 100 万个文件,因此该脚本需要大量内存。

http://ask.metafilter.com/134940/PowerShell-recursive-processing-of-all-files-and-folders-without-OutOfMemory-exception

我所需要的只是文件的名称、大小和位置。

从现在起我所做的就是:

$filesToIndex = Get-ChildItem -Path $path -Recurse | Where-Object { !$_.PSIsContainer }
Run Code Online (Sandbox Code Playgroud)

它有效,但我不想惩罚我的记忆:-)

最好的问候,新手

directory powershell

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

Powershell 固定宽度导出

我有一个文本文件,它使用固定宽度来分隔列。

我正在加载文件并创建一个新列,该列连接前两列的值。我遇到的问题是,在导出数据时,我需要为 C 列定义固定的列宽 13。

Column A (3)   Column B(9)   Column C(13)
MMA            12345         12345_MMA
MMO            987222        987222_MMO
Run Code Online (Sandbox Code Playgroud)

基本上,对于导出中的这个示例,我在第一行缺少 4 个空格,在第二行缺少 3 个空格。

这是我当前的代码,其中还包括用于创建 MD5 的新行。

    # Load input data
$PreSystem = [IO.File]::ReadAllText("C:\FILE.txt")

# Initiate md5-hashing
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding

# Split input data by lines
$all = $PreSystem.split("`n") 

# Loop over lines
for($i = 0; $i -lt $all.length-1; $i += 1) {
  # Access distinct lines
  $entry = "$($all[$i])"
  # Get the different …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0 fixed-width

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

将 Get-Date 与日期作为字符串进行比较

注册表包含一个键,其字符串值类似于:

23-01-2015 14-58-00
Run Code Online (Sandbox Code Playgroud)

使用 PowerShell,我需要比较此日期以确保它大于从(Get-Date).AddDays(-1). 但是格式不同,我无法在两者之间进行比较,也无法格式化 Get-Date,因为无法.AddDays针对字符串运行。有任何想法吗?

powershell

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

Powershell 从 txt 文件中的查询调用 sqlcmd

我在文件夹中的多个文本文件中有 TSQL 插入语句。我需要在 powershell 中使用 foreach 循环在 sql server 上运行。它应该转到文件夹中的每个文件读取查询执行该查询并再次终止连接读取下一个文件并执行相同的操作

invoke-sqlcmd -ServerInstance KUMSUSHI7 -Query (Get-Content | For-each "C:\Drill\Task\SAMS Automation\Query.txt")
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题

sql t-sql sql-server powershell

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

是否有理由显式导入模块?

我刚刚阅读 PowerShell模块指南页面,注意到该Import-Module部分有一行:

以下操作会触发模块的自动导入,也称为“模块自动加载”。

  • 在命令中使用 cmdlet。例如,键入 Get-ExecutionPolicy 会导入包含 Get-ExecutionPolicy cmdlet 的 Microsoft.PowerShell.Security 模块。

既然如此,我们为什么要关心使用Import-Module呢?它不是总是自动照顾我们吗?在什么情况下我需要明确写出Import-Module?

powershell import-module powershell-module

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

如何使用 Powershell 从活动目录中获取独特的部门?

我想使用 PowerShell 从 Active Directory 获取独特部门的列表。

当前代码:

Import-Module activedirectory
get-aduser -filter * -property department |select department | sort-object property -unique
Run Code Online (Sandbox Code Playgroud)

这将返回一个标题为“department”的列表,其中没有任何数据。如何获取所有部门的列表?

powershell active-directory

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

如何循环AD中的OU将用户添加到各个安全组?

我对编写 PS 脚本有点陌生,通常只需要简单的一两个衬里,但为此我尝试循环遍历 Active Directory 中的特定 OU 来查找每个用户部门,在其前面添加“grp”并添加他们通过该名称添加到安全组。例如,一个部门可能类似于 10005,所以我想将它们添加到名为“grp10005”的安全组中。这是我所拥有的,但它不起作用。

Import-Module ActiveDirectory  

 $users = $i = $null  
 $strCC
 $strGRP = 'grp' & strCC

 $users = Get-ADUser -SearchBase "ou=Test,ou=OurUsers,ou=Logins,dc=domain,dc=com" -filter * {department -eq $strCC}  
 ForEach($user in $users)  
    {    
      Add-ADGroupMember 'strGRP' -Members $_.DistinguishedName
        -ErrorAction SilentlyContinue
      $i++  
    }  
Run Code Online (Sandbox Code Playgroud)

powershell active-directory

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

启动时运行 powershell 脚本 (.ps1)

这个问题让我挠头。对于 Powershell 来说也很陌生。

\n\n

目标:机器启动时,Powershell 脚本启动并询问计算机名称,然后将其添加到域中,进行清理并重新启动机器。

\n\n

这是我的脚本:

\n\n
$getName = Read-Host -Prompt "Enter the new name for this computer"\n$user = "mdt_join"\n$password = Read-Host -Prompt "Enter password for $user" -AsSecureString\n$username = "bc\\mdt_join"\n$credential = New-Object System.Management.Automation.PSCredential($username,$password)\n\nAdd-Computer -NewName $getName -DomainName DOMAIN -OUPath "OU=Managed Stations,DC=DC,DC=DC,DC=DC" -Credential $credential -restart\n\nRename-Computer \xe2\x80\x93NewName $getName\n\nRemove-Item C:\\Users\\ladmin\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\startup.cmd\n\nRemove-Item $MyINvocation.InvocationName\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我手动运行该脚本,但是当我使用此startup.cmd时,该脚本可以工作

\n\n
REM   Attempt to set the execution policy by using PowerShell version 2.0 syntax.\nPowerShell -Version 2.0 -ExecutionPolicy Unrestricted .\\script1.ps1 >> "%TEMP%\\StartupLog.txt" 2>&1\n\nIF %ERRORLEVEL% EQU -393216 (\n   REM   PowerShell …
Run Code Online (Sandbox Code Playgroud)

powershell startup

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