Solved

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

Posted on 2016-08-24
8
30 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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