标签: powershell

递归删除 desktop.ini 文件

我正在尝试删除desktop.ini给定目录中的所有文件;即Documents\Gio。我试着del /S desktop.ini和del /S *.ini在CMD(管理模式),get-childitem .\ -include desktop.ini -recurse | foreach ($_) {remove-item $_.fullname}并get-childitem .\ -include *.ini -recurse | foreach ($_) {remove-item $_.fullname}在PowerShell中(也管理员)。两者都没有工作。我该怎么办?

这些desktop.ini文件包含以下内容:

[.ShellClassInfo]
InfoTip=This folder is shared online.
IconFile=C:\Program Files\Google\Drive\googledrivesync.exe
IconIndex=12
Run Code Online (Sandbox Code Playgroud)

我从我的 Google Drive 文件夹中移动了目录,但所有文件夹上仍然有共享图标。我试图将目录及其所有子目录和文件的所有权更改为不同的帐户。我尝试使用 Google Drive 执行此操作,但只有根目录更改了所有权;我无法删除其中的任何文件或目录。

powershell cmd delete-file

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

Powershell 哈希表存储“System.Collections.DictionaryEntry”的值

当我执行命令时:

$var = @{a=1;b=2}
Run Code Online (Sandbox Code Playgroud)

在 Powershell(版本 3)中,$var最终的值为{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}. 为什么会这样?如何存储我想要存储的值?

powershell powergui powershell-3.0

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

Powershell Out-File:强制编码为 Ascii

我正在尝试使用 Powershell 规范化一组制表符分隔的日志文件。

这是当前的脚本:

(Get-ChildItem *.csv) |%{
#Store the name of the file & date
$Name = $_.basename
$FileDate = $_.CreationTime
#Prepends the following to each message: unique Identifer, Hostname, date
(Get-Content $_.fullname) -replace "^","AR-LOG|$Name|$FileDate|"|
#Replaces the TAB delimeter with a Pipe delimeter
Foreach-Object {$_ -replace '   ','|'}  |
#Appends the resulting message in ascii
Out-File -append -FilePath normalized.log -Encoding ascii
Run Code Online (Sandbox Code Playgroud)

输入和输出的片段可以在这里看到:

http://pastebin.com/uaQadYUC

如何强制输出文件为 ascii 而不是某种类型的 unicode?

*** 编辑:进一步的故障排除表明输入文件实际上是 windows-1252 编码的,显然 Get-Content 无法本地处理(?)

powershell character-encoding

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

用于将特定 PDF 页面打印成图像的 Powershell 脚本

我如何更改此 powershell 脚本

Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
Run Code Online (Sandbox Code Playgroud)

到:

  1. 打印 PDF 的特定页面,
  2. 直接到图像(例如 png、jpg、tif 等),以及
  3. 相应地保存它们?

例如,我想将 ABC.pdf 的第 3、4、7 页打印成三个单独的文件,分别称为 ABC_3.png、ABC_4.png 和 ABC_7.png;图像文件可以是任何格式(.png、.jpg、.tif 等)。

我计划调用 .csv 列表来获取所有参数值(例如要打印的页码、带页码的输出名称、新文件位置的文件路径等),但我不知道如何设置 powershell 语法。谢谢你。

更新:

我使用下面的脚本在此任务上取得了进展,该脚本调用了 ghostcript。除了我似乎无法将我的 -dFirstPage 和 -dLastPage 设置为来自我的 csv 的参数之外,它执行了 1-3 项操作……我收到了 powershell 错误:

Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
Run Code Online (Sandbox Code Playgroud)

如果我用一个数字替换 $pg ,它似乎工作正常。我将如何使用 -sNAME 来解决这个问题?

新脚本

#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'

$files = Import-CSV …
Run Code Online (Sandbox Code Playgroud)

printing pdf powershell ghostscript

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

将小的 PS 脚本转换为 .BATch 文件中的长行

我有这个 PowerShell 代码,是从这个问题的答案中得到的;它显示了运行 PS 代码的 cmd.exe 窗口的位置/尺寸:

$WindowFunction,$RectangleStruct = Add-Type -MemberDefinition @'
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}
'@ -Name "type$([guid]::NewGuid() -replace '-')" -PassThru

$MyWindowHandle = (Get-Process -Id (Get-WmiObject Win32_Process -Filter "ProcessId=$PID").ParentProcessId).MainWindowHandle

$WindowRect = New-Object -TypeName $RectangleStruct.FullName
$null = $WindowFunction::GetWindowRect($MyWindowHandle,[ref]$WindowRect)

Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom
Run Code Online (Sandbox Code Playgroud)

当我从命令行在 .ps1 脚本中运行此代码时,它可以正常工作:

C:\Users\Antonio\Documents\test> powershell Set-ExecutionPolicy …
Run Code Online (Sandbox Code Playgroud)

windows powershell cmd batch-file

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

如何在 Windows IoT 中记录事件?

Windows IoT 的 WebUI(如 Raspberry PI 所示)具有以下事件跟踪屏幕:

在此处输入图片说明

如何从 uwp 程序写入事件日志,然后检查其中写入的内容?

此答案(与 Windows IoT 无关)表明您需要为其使用标准 System.Diagnostics.Tracing 类。此答案所指的示例是演示日志记录到文件。在登录到内置工具(例如 ETW)后,我更喜欢。

遵循与示例中类似的逻辑,我正在执行EventSource.Write并且它不会抛出错误,但无处可见写入的消息。

适用于 Window IoT 的 Windows Powershell 也不支持 Get-EventLog 命令。

Windows IoT 开发相对较新,所以网上资料不多。目前最好的来源是Ms Iot Samples,但尽管样本数量庞大,但似乎没有人进行任何事件记录。

c# powershell etw win-universal-app windowsiot

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

Powershell Invoke-Sqlcmd to DataTable rowcount 在其 1 条记录时不正确

$sql=("select top 1 * FROM CollectionProfile")

$CollectionProfile = New-Object System.Data.DataTable

$CollectionProfile = Invoke-Sqlcmd -ServerInstance $Instance -Database $db -Query $sql -ErrorAction Stop  

$CollectionProfile.Rows.Count
Run Code Online (Sandbox Code Playgroud)

回报:0

但是,如果我将 TOP 计数更改为 2 -

$sql=("select top 2 * FROM CollectionProfile")
Run Code Online (Sandbox Code Playgroud)

回报:2

让我发疯,是的,我在“内网”上找不到对此的任何引用。我一定是做错了什么,但是什么?

sql-server powershell

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

Powershell 计划任务“延迟任务”选项

我需要使用“延迟任务”选项创建任务计划 1 分钟。我可以看到 PowerShell cmdlet New-ScheduledTaskTrigger 有一个选项 -RandomDelay,但我认为它无效。我也看不到文档中的另一个选项(https://technet.microsoft.com/en-us/library/jj649816(v=wps.630).aspx)。

$DELAY = new-timespan -minutes 1
$T = New-ScheduledTaskTrigger -AtLogon -RandomDelay $DELAY
Run Code Online (Sandbox Code Playgroud)

powershell

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

从 Powershell 到逗号分隔字符串的结果

我运行了以下脚本,如何将输出放入逗号分隔的字符串中?

Get-ADGroup -filter * -properties GroupCategory | ft objectguid, samaccountname
Run Code Online (Sandbox Code Playgroud)

电流输出

objectguid                                                  samaccountname
----------                                                  --------------
f5b71a40-9405-4874-b9b0-b35e45346f63                        WinRMRemoteWMIUsers__
95a99aa1-a771-4f86-bdce-7a6a3f00057a                        Administrators
6a6ad877-180c-462b-9672-bfba20200cda                        Users
038b03cf-1171-4546-b0af-a7654823e289                        Guests
ed3995ae-6f77-492f-bcb5-bc197fa24996                        Print Operators
b1f8301a-78e1-47ab-9e51-8e55525acadf                        Backup Operators
3d23c4c0-71d9-4e08-a8ee-a9692a6ed61f                        Replicator
d86e5619-5893-41b7-b3a8-eda4c2725562                        Remote Desktop Users
473592e4-71c8-458f-b7fc-b474809933ff                        Network Configuration Operators
c433a049-f9e5-404a-9940-5cf1f8ce4d82                        Performance Monitor Users
1ce2daf3-5d97-4493-9ec1-c84a49326113                        Performance Log Users
24b06a4c-f32b-4081-a002-6f1851f98256                        Distributed COM Users
Run Code Online (Sandbox Code Playgroud)

期望输出

f5b71a40-9405-4874-b9b0-b35e45346f63,WinRMRemoteWMIUsers__,95a99aa1-a771-4f86-bdce-7a6a3f00057a,Administrators,6a6ad877-180c-462b-9672-bfba20200cda,Users,038b03cf-1171-4546-b0af-a7654823e289,Guests,ed3995ae-6f77-492f-bcb5-bc197fa24996,Print Operators,b1f8301a-78e1-47ab-9e51-8e55525acadf,Backup Operators,3d23c4c0-71d9-4e08-a8ee-a9692a6ed61f,Replicator,d86e5619-5893-41b7-b3a8-eda4c2725562,Remote Desktop Users,473592e4-71c8-458f-b7fc-b474809933ff,Network Configuration Operators,c433a049-f9e5-404a-9940-5cf1f8ce4d82,Performance Monitor Users,1ce2daf3-5d97-4493-9ec1-c84a49326113,Performance Log Users,24b06a4c-f32b-4081-a002-6f1851f98256,Distributed COM Users
Run Code Online (Sandbox Code Playgroud)

我将它传递给一个字符串并使用 JavaScript 远程解析数据。

powershell

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

检测脚本是否在 Azure 自动化中运行的正确方法是什么?

使用 Azure 自动化开发 PowerShell 脚本可能会非常缓慢。这可以通过使用 PowerShell ISE 插件来帮助实现,该插件允许您在本地测试运行脚本。

但是,在本地运行与在 Azure 自动化中运行时,某些事情不可避免地会有所不同。例如文件路径。

检测脚本当前运行的环境的正确方法是什么?

目前我定义了一个变量资产,我只保留本地而不上传。然后我可以执行以下操作:

# Check if we are running locally - NOTE: Do not upload the runningLocally variable! Keep it local only
if (Get-AutomationVariable -Name 'runningLocally') { 
    # We are running locally
} else { 
    # We are running in Azure Automation
}
Run Code Online (Sandbox Code Playgroud)

但这似乎相当笨拙且容易出错。我正在寻找一种更强大和可靠的方法。

我发现了一些额外的方法。AA运行时的机器名和用户名都是“客户端”,这似乎是一个更健壮的方法?

powershell azure-automation

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