Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-24
8
Medium Priority
?
37 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 18

Accepted Solution

by:
Learnctx earned 2000 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 18

Expert Comment

by:Learnctx
ID: 41836680
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

An article on effective troubleshooting
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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