Solved

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

Posted on 2016-08-24
8
27 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

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!

Question has a verified solution.

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

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

730 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