Solved

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

Posted on 2016-08-24
8
19 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:prologic08
Comment Utility
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
Comment Utility
Does anyone have an idea how to pass the TICKETNUMBER value in SCCM?
0
 
LVL 16

Accepted Solution

by:
Learnctx earned 500 total points
Comment Utility
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 16

Expert Comment

by:Learnctx
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now