I am trying to display excel data(text format) present across 10 columns in powershell console
But it is not working
$objExcel = New-Object -ComObject Excel.Application
$objexcel.visible=$true
$objexcel.DisplayAlerts=$false
$workbook=$objExcel.Workbooks.open("C:\Users\Desktop\commute.xlsx")
$worksheet=$workbook.sheets.item(1)
$usedrange=$worksheet.UsedRange
#{TIME=$worksheet.Range("A1:A10").text
DEPART=$worksheet.Range("B1:B10").text
REDS=$worksheet.Range("C1:C10").text
TRAINS=$worksheet.Range("D1:D10").text }
why don't you use the cmdlet Import-CSV? It will be much easier.
try the following:
$Worksheet = Import-Csv -Path 'C:\Users\Desktop\commute.xlsx'
It will create a Powershell object you can use and manipulate.
hope it helps you.
Related
I'm new to power shell and can't seem to get this script correct.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$users1\$users.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$users"
$Shortcut.Save()
Every time it goes to "users.txt" it tries to read it as a complete file. Instead i would like for it to read as followed:
User1
User2
User3
I just can't seem to get this to work.
RE-EDIT:
What I’m trying to accomplish:
We went from one storage server to a new storage server.
ASRV1 to FSSRV
I want to create a shortcut from asrv1 to fssrv. Along with the upgrade we are also changing domains and changing everyone windows user names.
For example my user name was fMunoz and it was changed to fMunoz00.
I want to pull from a text file > users.txt with all the old usernames, and create shortcuts to the new user names storage file those users names are in a txt file called users1.txt.
If I understand your request, you want to create shortcuts in each home directory of the people in Users1.txt
This means you will need to complete multiple ForEach loops to cycle through both txt files.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
# For every line in $users1, Do something
Foreach($User in $users1) {
# For every line in $users, Do something
Foreach($Name in $users) {
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$User\$Name.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$Name"
$Shortcut.Save()
}
}
I want to thank everyone for helping me.
I came up with a different solution for my problem. The way i fixed this issue was by creating an excel sheet w/ 2 columns.
OLD USERS | NEW USERS
I then created a mail merge using microsoft word. Then i continued with the following code in powershell to start my loop:
$WshShell = New-Object -comObject WScript.Shell
Once my loop was started with i then utilized mail merge to take <> from the excel sheet and replaced it with the insert it into my code. Also took <> and did the same.
$Shortcut = $WshShell.CreateShortcut("\Fssrv\homeshares$\«New_Users»\«Old_Users»_Z Drive.lnk")
$Shortcut.TargetPath = "\asrv1\users\«Old_Users»"
$Shortcut.Save()
I hope this can help anyone else trying to accomplish something similar.
Yet again, Thanks & Good Luck...
I'm Trying to import 2 different CSV files into an excel workbook on 2 different worksheets.
I've figured out how to create an excel workbook and import one of the CSV files. But when creating a new worksheet, I can't seem to import the 2nd CSV file into the new worksheet the same way that I imported the first CSV file.
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel = $Excel.Workbooks.open(C:\Temp\CSVfile1.csv)
#Create new worksheet and import 2nd CSV file
$Excel.Worksheets.add()
$Excel.Sheets(1).Name = 'Errors'
$Excel2 = $Excel.Workbooks.open(C:\Temp\CSVfile2.csv)
The first CSV file import is successful. The 2nd worksheet is created, but the 2nd csv file is not imported.
Error message displayed is
You cannot call a method on a null-valued expression.
I highly recommend using the ImportExcel module for any Excel-related files, it makes them much easier to work with and only takes a couple seconds to get it working. Moreover it has the benefit of using reading and editing Excel files without needing Microsoft Excel to be installed.
See the following example to do what you're looking for using the ImportExcel module:
Import-Csv -Path 'C:\Temp\CSVfile1.csv' | Export-Excel -Path 'C:\Temp\ExportExcelTest.xlsx' -WorkSheetname 'Sheet1'
Import-Csv -Path 'C:\Temp\CSVfile2.csv' | Export-Excel -Path 'C:\Temp\ExportExcelTest.xlsx' -WorkSheetname 'Errors'
Additionally, ImportExcel also comes along with many formatting options which may be of use to you. I can't recommend it enough over using a ComObject to modify an Excel file.
i have found this script online. It converts docx files to pdf. The thing is, it creates one pdf for each docx. I need to edit this script, to merge 2 docx files into one single PDF file. I have zero knowledge of powershell, but i know batch in linux.
$documents_path = Split-Path -parent $MyInvocation.MyCommand.Path
$word_app = New-Object -ComObject Word.Application
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {
$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
$word_app.Quit()
This is the design of the script you are using.
Use the more direct approach by merging the .docx files first, then convert to PDF. This means you have to understand the MSWord object model and how to code for it. You're going to have to pick a starting .docx the append other word data to the end.
So, do a search for how to merge Word files. Get that worked out, then you can just use PowerShell to make them .pdfs.
With zero knowledge of PowerShell, you should really take a few quick online training session to get an handle on it all, before you get yourself in a very frustrating position.
Go to the Microsoft Virtual Academy and YouTube and do a search for 'beginning PowerShell'
I have a PowerShell script that I wrote a few years ago and it worked great...until it didn't.
$shell = New-Object -com Shell.Application
$filepath = 'C:\Billing\Clients'
$shell.Namespace($filepath).Items() |
% { $_.InvokeVerb('Print') }
In the C:\Billing\Clients folder I copy ~100 Excel files. Each of these Excel files need to be printed, in the alphabetical order of the file name.
This was working great until this month. I guess an update to Excel changed things.
Now the script tries to open and print all of the Excel files at the same time. Previously it printed the files in serial.
This was awesome. Now it brings my system to it's knees and documents are printed in a random order.
Any ideas on how I can invoke the Print operation and wait for it to complete prior to calling the Print operation on the next file?
I have used this in the past to pull files into excel and print them. You will need to ensure your default print settings are set first or use $xl.ActivePrinter = "PRINTERNAME AS EXCEL SEES IT" to set it first.
#Open Excel
$xl = new-object -comobject excel.application
#don't show the window
$xl.visible = $false
#get all of your files
get-childitem "C:\SOME\Path" "*.xlsx" | foreach-object {
#open file
$wb = $xl.Workbooks.Open($_.FullName)
#print with defaults
$wb.PrintOut()
#close without saving any changes
$wb.Close($false)
}
#all done so close excel
$xl.Quit()
I am trying to use the invoke (ii) command to open an access database that has command line options. What I would like to have executed is below (yes there is a space in the name of the access database). The database is in the same folder as the Powershell script.
What I want: program name.accdb /cmd Rester
What I get: program name.accdb \cmd Rester
The exact commands I am using are:
$Path_To_EXE = "program name.accdb /cmd Rester"
&ii $Path_To_EXE
I am new to Powershell and have done some searching but can't seem to find an answer. I can create a work around by creating a separate .bat file but that seems like going backwards.
Thoughts?
You should also give a shot to the start-process cmdlet :
$Path_To_EXE = "c:\program.exe"
#Notice the simple quotes ...
$Arguments = #( "name.accdb", '/cmd' , "Rester" )
start-process -FilePath $Path_To_EXE -ArgumentList $Arguments -Wait
I'm not quite sure of the format of the answer you'll get tough ...
for database interaction, I'll rather use JGreenwell's Approach, since the answer that you'll get will be much easier to read/debug ...
Let me know if it works.
If you want to run a VBA script while passing it a parameter with powershell:
$aApp = New-Object -ComObject access.application
$aApp.Application.OpenCurrentDatabase("some program.accdb")
$aApp.Application.Run("VBAScriptName", [ref] "Raster")
First according to Microsoft Support you can use ;; for /cmd from the command line. Second because of the way call quotes and dequotes variables you have to include the /cmd flag separate from the variable (well, its the easiest way). Third, you might consider creating a new com-object to handle running Access with Powershell as it allows for a lot more options (just ask and I can add some examples of this). This being said try:
$Path_To_EXE = "program name.accdb"
&ii $Path_To_EXE ;;Rester #Try ;;"Rester" if it doesn't work.
#if that works then its a problem in Rester
#fyi another way is:
$Path_To_EXE = #("program name.accdb", ";;Rester")
&ii $Path_To_EXE
If you want to use an ActiveX Object Controller to open and perform operations on Access look at this blog from technet <- Read the link there are pitfalls to avoid.
$adOpenStatic = 3
$adLockOptimistic = 3
$objConnection = New-Object -com "ADODB.Connection"
$objRecordSet = New-Object -com "ADODB.Recordset"
$objConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Scripts\Test.mdb")
$objRecordset.Open("Select * From Computers", $objConnection,$adOpenStatic,$adLockOptimistic)
$objRecordSet.AddNew()
$objRecordSet.Fields.Item("ComputerName").Value = "atl-ws-001"
$objRecordSet.Fields.Item("SerialNumber").Value = "192ATG43R"
$objRecordSet.Update()
$objRecordSet.Close()
$objConnection.Close()