Solved

Convert VBS to Powershell - Copies a file from one network location to another using SCCM

Posted on 2016-08-24
8
23 Views
Last Modified: 2016-10-10
Recently I was requested to change the below to PowerShell because I was having problems getting it to pass the value that gets entered when we do OS Deployment. We think it may work if we try running it as Powershell. Can someone help please? Thank you!

dim filesys, objEnv
set filesys=CreateObject("Scripting.FileSystemObject")
set objEnv=CreateObject("Microsoft.SMS.TSEnvironment") 
strTicketNumber = objEnv("TICKETNUMBER")
If filesys.FileExists("\\CONTOSO\Documentation\file.xlsm") Then
filesys.CopyFile "\\CONTOSO\Documentation\file.xlsm", "\\CONTOSO\Documentation\new folder\" & strTicketNumber & ".xlsm"
End If

Open in new window

0
Comment
Question by:prologic08
  • 3
  • 2
  • 2
8 Comments
 
LVL 12

Expert Comment

by:Benjamin Voglar
ID: 41769263
Copy-Item -Path \\server\share -Filter *.xlsm -Destination \\server2\share –Recurse

$test = test-path "\\CONTOSO\Documentation\file.xlsm"

if ($test -eq "True")  {Copy-Item -Path "\\CONTOSO\Documentation\file.xlsm"  -Destination "\\CONTOSO\Documentation\new folder\"}

Open in new window


Here is how to deploy powershell with sccm:

https://community.spiceworks.com/topic/1369218-how-use-sccm-to-run-powershell-script-on-user-s-pc
0
 

Author Comment

by:prologic08
ID: 41769405
Thank you Benjamin,

Will this pull the value of TICKETNUMBER? And do I need to add
Copy-Item -Path \\server\share -Filter *.xlsm -Destination \\server2\share –Recurse

Open in new window



So I assume It should look like this:

Copy-Item -Path \\server\share -Filter *.xlsm -Destination \\server2\share –Recurse
$test = test-path "\\CONTOSO\Documentation\file.xlsm"
if ($test -eq "True")  {Copy-Item -Path "\\CONTOSO\Documentation\file.xlsm"  -Destination "\\CONTOSO\Documentation\new folder\"}

Open in new window

0
 
LVL 12

Expert Comment

by:Benjamin Voglar
ID: 41769807
Oh i'm sorry. I made a mistake when I copy paste the script when i tested it.

It should be:

$test = test-path "\\CONTOSO\Documentation\file.xlsm"
if ($test -eq "True")  {Copy-Item -Path "\\CONTOSO\Documentation\file.xlsm"  -Destination "\\CONTOSO\Documentation\new folder\"}

Open in new window

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:prologic08
ID: 41770191
Thank you Benjamin. Will this pull the value of TICKETNUMBER? I want it to change the name of the file to the ticket number that I enter before OSD deployment. I think this will only copy a file.
0
 

Author Comment

by:prologic08
ID: 41774731
Does anyone have an idea how to pass the TICKETNUMBER value in SCCM?
0
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 41779064
Try this.

$objEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$strTicketNumber = $objEnv.Value("TICKETNUMBER")
$FilePath = "\\contoso\documentation\file.xlsm"
$FileDest = "\\contoso\documentation\new folder\$($strTicketNumber).xlsm"

if (test-path -path $FilePath)
{
	try
	{
		Copy-Item -Path $FilePath -Destination $FileDest -ErrorAction Stop
	}
	catch
	{
		## Handle error
	}
}

Open in new window

0
 
LVL 17

Expert Comment

by:Learnctx
ID: 41836680
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question