I am running a script to listen for new files in a specific folder. When it finds a new file I want email that file, and another file that I'm explicitly defining. here's a bit of code :
$Report = "C:\somefolder\somefile.xml"
$AnotherReport = "C:\somefolder\someotherfile.xml"
And a bit later I want to do something like this
Send-MailMessage -Attachments $Report,$AnotherReport
I want to add more than one attachment from different locations. What is the proper syntax to do that?
I have not tested but you can try like this
#Converts to array
$Report = #("C:\somefolder\somefile.xml")
$AnotherReport = "C:\somefolder\someotherfile.xml"
$Report += $AnotherReport
Send-MailMessage -Attachments $Report
Related
Here is a very simplified version of Powershell script I'd like to use to return ADUser properties (in a CSV) such as name, givenName, Office etc for users in a given OU.
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'Get-ADUser Properties'
$msg = 'Enter desired User properties, each seperated by a comma:'
$default = "name, office"
$propertiesSought = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, $default)
get-aduser -filter * -properties * -searchbase $OU | select $propertiesSought | Export-Csv -Path "C:\Users\Administrator\Desktop\temp.csv"
The script prompts the user to enter the comma separated properties into an InputBox, which is then saved as variable. On the saved CSV file, all I can see is "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" for each of the users' details i was expecting to see...
I've tried to isolate the problem as much as I can. If the text submitted is a single word such as "name" then everything is OK. If I add anything more, even without spaces, the problem comes back.
Also, when I run the script in ISE and choose not to output to a CSV file, on each line of the console all I"ll get back is a set of empty braces for each user in the OU.
Getting this fixed, would mean a lot to me. Thanks.
I've tried to isolate the problem as much as I can. If the text submitted is a single word such as "name" then everything is OK. If I add anything more, even without spaces, the problem comes back.
You're really close!
Select-Object accepts an array of property names or property expressions - not a single comma-separated string.
When you pass the string "name,email,manager" to Select-Object, it starts looking for a property with the literal name name,email,manager - as if you'd tried to access $user."name,email,manager".
Split the string into individual property names and it will work:
# ...
$propertiesSought = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, $default)
# Split input string
$propertiesSought = $propertiesSought -split ',\s*'
Get-ADUser -Filter * -Properties $propertiesSought -searchbase $OU |Select $propertiesSought |Export-Csv ...
How to show uploaded files using multiple upload files?
With data like this
Lets consider you name is in this variable = $uploadedFiles;
$uploadedFiles = ["file1","file2","file3"];
$temp = json_decode($uploadedFiles);
foreach($temp as $name) {
echo $name;
}
It will print your file name 1 by 1
It just names you need the files also
but if you want to store only that names then you can do it by taking a loop for that variable array
if it is not an array then convert to array using json_decode(array)
I am trying to generate an Excel file with CodeIgniter and the PhpSpreadsheet library from a view. The reports that I need to make are not a list but much more complex and I can generate them more quickly with a view and sending parameters. This is my code:
$data = $this->model->bringdata();
$view = $this->load->view("data_view", $ data);
$spreadsheet = new Spreadsheet();
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
$writer-> save($view);
And the following error is shown
Severity: Warning
Message: fopen (): Filename can not be empty
Filename: Writer / Html.php
Line Number: 160
In the PhpSpreadsheet documentation it shows that an html page must be explicitly sent:
$writer->save("05featuredemo.htm");
Is there any way to do what I need?
Thanks for the help
It's a simple solution, you need to pass true to the third argument of load->view in order for it to return a string rather than sending it to the browser.
$view = $this->load->view("data_view", $ data, true);
Added after comment
I made the mistake of not looking into how the Html writer works.
The HTML writer writes a spreadsheet as an html file. It does not create a spreadsheet. As I understand it (and I might be mistaken) you can use \PhpOffice\PhpSpreadsheet\Reader\Html to read an html file into a spreadsheet.
In your case I think you will have to save the string returned from $this->load->view() to a temporary file. CodeIgniter's file_helper might be useful here.
You then read the temporary file to create a spreadsheet. After that you can use $writer to save the spreadsheet somewhere as any of the file types the PhpSpreadsheet supports.
$view = $this->load->view("data_view", $data, true);
$this->load->helper("file");
$fileName = "temp_file_name.html";
$path = "full/path/to/some_writable_folder/";
$path_file = $path . $fileName;
if (write_file($path_file, $view))
{
//create spreadsheet the temp html
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->load($path_file);
//write out to html file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
$writer->save($path."05featuredemo.htm");
//delete the temporary file
unlink($path_file);
}
else
{
//handle the failure to write the temp file
}
I haven't tested this and I'm not sure you really want to save to an html file, but I think the above is close to the answer.
Second addition
If you want to save .xlsx files you need an Xlsx writer. Use the following instead of what's shown above.
//write out to excel file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($ path. "doc.xlsx");
I confess that I have never used, or considered using the html reader so I'm not sure you'll get what you want. I honestly think that creating an html table so you can import it into excel is probably doing it the hard way.
If you open the properties of a file in Windows, there usually is a Details tab. I want to access the information on this tab, but I don't know how.
Is there a module for it? Does someone has a code sniplet?
I tried to work with Win32::File's GetAttributes, but these are not the attributes I was looking for.
use Win32::OLE;
my $objShell = Win32::OLE->new("Shell.Application") or die;
my $objFolder = $objShell->NameSpace($myDir) or die;
my $objFile = $objFolder->ParseName($fileName) or die;
while ( $i <= 34 )
{
my $propertyName = $objFolder->GetDetailsOf($fileName,$i);
my $propertyValue = $objFolder->GetDetailsOf($objFile,$i);
print "$i -- $propertyName -- $propertyValue\n";
$i++;
}
You can instantiate a COM "Shell.Application" object. It exposes a .NameSpace(folder) method that returns a reference to the name space of the indicated folder, which holds the information you need. The retrieved instance holds a Items collection with references to each of the files in the folder, and a .GetDetailsOf(file,property) to retrieve each of the values seen in the details tab and explorer columns.
Sorry i have no idea of perl, so i can not include any working code.
Is there a shorter way to set multiple properties to the same value in Powershell in one command than this?
Example:
(gi "c:\test.txt").LastWriteTime = (gi "c:\test.txt").LastAccessTime = (gi "c:\test.txt").CreationTime = Get-date
I'm just curious if there is a way to shorten this syntax.
"CreationTime","LastWriteTime","LastAccessTime" |% {(gi test.txt).$_ = (get-date)}
I've used a slightly modified version of Mjolinor's answer to solve a problem I had of incorrect date on files that had just been downloaded from a remote source. I modified the code to make it cleaner to understand in case I have to come back to in the future (changed the short hand to full command names).
# Correct Access/Create/Write times on transferred files
ForEach( $File in $TransferList ) {
#("CreationTime","LastAccessTime","LastWriteTime") | ForEach {
$(Get-Item $File.Name).$_ = $File.Date
}
}