Powershell脚本,用于检查reg密钥并将文件或文件夹复制到位置

时间:2021-04-16 23:45:48

Trying to write a script for Location based printing in a VDI environment. Upon login to the VDI session, a network printer will get mapped based on the client's (thin/zero client) IP (using GPO). Based on that printer name (ie.'PRN-Printer1'), I need to copy a file from a share (\Server\share\printer1) to the local c:\drive\location. For 'PRN-Printer2', it will copy from \Server\share\printer2, etc..

尝试在VDI环境中编写基于位置的打印脚本。登录VDI会话后,网络打印机将根据客户端(瘦/零客户端)IP(使用GPO)进行映射。基于该打印机名称(即'PRN-Printer1'),我需要将文件从共享(\ Server \ share \ printer1)复制到本地c:\ drive \ location。对于'PRN-Printer2',它将从\ Server \ share \ printer2等复制。

I found a few scripts the check for the reg key and another that copies the files but I need some help putting it all together. I am not an expert in scripting but I can fumble my way through some one liners.

我找到了一些脚本检查reg键和另一个复制文件,但我需要一些帮助把它们放在一起。我不是脚本专家,但我可以通过一些内容来摸索。

Any assistance would be greatly appreciated!!!

任何帮助将不胜感激!!!

Here is what I have so far...

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

Check registry (Not sure this is the best way to look for a key)

检查注册表(不确定这是查找密钥的最佳方法)

reg query HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers  /f "PRN-" /t REG_SZ /s /k  | find "Name"

or

pushd;sl HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers; if(test-path PRN-){--???--}ELSE{"Printer does not exist"};popd

to copy the file:

复制文件:

ls "C:\drive\location" -r -i * | % {cp -force $_ ($_ -replace "c:\\drive\\location", "\\Server\share1")}

or

Copy-Item \\Server\Share\printer1\* C:\drive\location\

Not sure how to put this logic together or the best way to check for this printer in the registry since there could be multiple printers on the VDI session.

由于VDI会话中可能有多台打印机,因此不确定如何将此逻辑放在一起或在注册表中检查此打印机的最佳方法。

Basically, I need something that does:

基本上,我需要做的事情:

If reg key (HCLM..) is 'PRN-printer1' then copy file(s) \Server\share\printer1 to c:\drive\location else... check for printer2....printer3...etc...

如果reg键(HCLM ..)是'PRN-printer1',则将文件\ Server \ share \ printer1复制到c:\ drive \ location else ...检查printer2 .... printer3 ...等。 ..

Please help! Thanks!

请帮忙!谢谢!

1 个解决方案

#1


0  

You can use Get-Item to get list of all printers and store it in a variable $printername

您可以使用Get-Item获取所有打印机的列表,并将其存储在变量$ printername中

$printername=(Get-Item "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers\*").pschildname

Heres what you will have in $printername

下面你将在$ printername中拥有什么

PRN-printer1
PRN-printer2
PRN-printer3

Now for each $printer in $printername you can execute below script to copy files based on printer

现在,对于$ printername中的每个$ printer,您可以执行以下脚本来根据打印机复制文件

ForEach($printer in $printername) {

if ($p -match  "PRN-printer1")

{
Copy-Item "\\Server\Share\printer1\*" "C:\drive\location"

}

elseif ($p -match  "PRN-printer1")

{

Copy-Item "\\Server\Share\printer2\*" "C:\drive\location"

}

elseif ($p -match  "PRN-printer1")

{

Copy-Item "\\Server\Share\printer2\*" "C:\drive\location"

}

}

#1


0  

You can use Get-Item to get list of all printers and store it in a variable $printername

您可以使用Get-Item获取所有打印机的列表,并将其存储在变量$ printername中

$printername=(Get-Item "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers\*").pschildname

Heres what you will have in $printername

下面你将在$ printername中拥有什么

PRN-printer1
PRN-printer2
PRN-printer3

Now for each $printer in $printername you can execute below script to copy files based on printer

现在,对于$ printername中的每个$ printer,您可以执行以下脚本来根据打印机复制文件

ForEach($printer in $printername) {

if ($p -match  "PRN-printer1")

{
Copy-Item "\\Server\Share\printer1\*" "C:\drive\location"

}

elseif ($p -match  "PRN-printer1")

{

Copy-Item "\\Server\Share\printer2\*" "C:\drive\location"

}

elseif ($p -match  "PRN-printer1")

{

Copy-Item "\\Server\Share\printer2\*" "C:\drive\location"

}

}