Powershell determine if a TIFF file is a Multi-TIFF - image

I'd like to know if it's possible to determine using Powershell if a TIFF file has several pages.
And if so, how can I do that?
Thanks.

whit something like this:
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$a = [System.Drawing.Bitmap]::FromFile( "C:\PS\multipage_tif_example.tif" )
if ($a.GetFrameCount( [System.Drawing.Imaging.FrameDimension]::Page ) -gt 1 )
{ "Tiff is multi pages" }
else
{ "Tiff is single page" }

Related

Checking the Online Statuses of Multiple Websites Simultaneously

I need some help with a basic PowerShell script that sends a 200 HTTP request to a website to check if it's online. I have a script that checks one website, but I need to update it in order to check multiple websites every time it runs. I need to check 8-10 websites at a time.
Thanks.
Here is my script so far:
$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is OK!"
}
Else {
Write-Host "The Site may be down, please check!"
}
If ($HTTP_Response -eq $null) { }
Else { $HTTP_Response.Close() }
I tried a few things. Added another HTTP_Request line, added another set of parentheses with the URL. I'm a complete novice when it comes to PowerShell, so any assistance would be greatly appreciated.
My approach would be to create an array of addresses and then loop through them. I'm not great with powershell syntax but something like...
$websites = #("www.google.com", "www.facebook.com", "www.amazon.com", "www.microsoft.com")
foreach ($site in $websites) {
Test-Connection $site -Count 1 -Quiet
if ($?) {
Write-Host "$site is up"
} else {
Write-Host "$site is down"
}
}

I'm not getting the expected outputs in this powershell script

i made this script in powershell, but i'm not doing something correctly (I'm trying to learn self-taught uwu)
This is the script in question.
function Get-DesiredProcess {
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
Switch ($DesiredProcess)
{
WUP {$ChosenProcess=Install-WUpdate}
MDP {$ChosenProcess=Set-Proxy}
}
If ($DesiredProcess -eq $null) {
Write-Error "You must specify an action!"
return Get-DesiredProcess
}
Else {
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
}
}
Get-DesiredProcess
Write-Output $DesiredProcess
Write-Output $ChosenProcess
The 2 last "Write-Output" are just for testing if it did registry the variables correctly or not (spoiler, it didn't)
When the Read-Host have not been answered, it should output "You specify an action!" but does nothing:
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification):
PS C:\Users\user1\Documents\scriptsfiles>
And when a choice is made, it should keep the variable with this part:
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
Instead of that it executes directly the choosen process...
Thanks in advance!
If you want your function to set a new value to a variable defined outside the function, use scoping inside the function, so
$ChosenProcess = 'Install-WUpdate' --> $script:ChosenProcess = 'Install-WUpdate'.
However, easier to understand is to have your function output something the rest of the code can deal with. In this case, since you want both what the user typed in, AND what the chosen process for that input would be, I would suggest having the function output an object that has both properties like:
function Get-DesiredProcess {
# inside the function both variables have LOCAL scope
$ChosenProcess = $null # initialize to nothing, or an empty string
$DesiredProcess = Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess) {
'WUP' {$ChosenProcess = 'Install-WUpdate'}
'MDP' {$ChosenProcess = 'Set-Proxy'}
default {
Write-Warning "You did not specify an accepted action. Type 'WUP' or 'MDP'"
Get-DesiredProcess
}
}
# only output if there is a matching chosen process
if (![string]::IsNullOrWhiteSpace($ChosenProcess)) {
# now have the function return both what the user typed and what the chosen action should be
[PsCustomObject]#{
DesiredProcess = $DesiredProcess
ChosenProcess = $ChosenProcess
}
}
}
$result = Get-DesiredProcess
Using this would output something like
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): 7
WARNING: You did not specify an accepted action. Type 'WUP' or 'MDP'
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): wup
DesiredProcess ChosenProcess
-------------- -------------
wup Install-WUpdate
Now, the following code can simply act on what is in variable $result.ChosenProcess
What I understand after reading your description, you need to declare all of the variables as global otherwise you need to access those variables inside of the function. A function variable can not be accessible from outside of the function. As per my understanding from your description, your code will be like this:
function Get-DesiredProcess
{
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess)
{
1 {$ChosenProcess="Install-WUpdate"}
2 {$ChosenProcess="Set-Proxy"}
}
If ($DesiredProcess -eq [string]::empty)
{
Write-Error "You must specify an action!"
Get-DesiredProcess
}
Else
{
Set-Variable -Name "DesiredProcessName" -Value "$ChosenProcess"
Write-host "You chose this: "$DesiredProcessName
}
}
Get-DesiredProcess

Get a file path from the explorer menu to a Powershell variable

I need to make a API call where file upload operation is required how can I prompt user to select the file from explorer and use the path after storing in a variable. I found similar question but it only works for folder.
On Windows, you can take advantage the OpenFileDialog Windows Forms component:
function Select-File {
param([string]$Directory = $PWD)
$dialog = [System.Windows.Forms.OpenFileDialog]::new()
$dialog.InitialDirectory = (Resolve-Path $Directory).Path
$dialog.RestoreDirectory = $true
$result = $dialog.ShowDialog()
if($result -eq [System.Windows.Forms.DialogResult]::OK){
return $dialog.FileName
}
}
Then use like so:
$path = Select-File
if(Test-Path $path){
Upload-File -Path $path
}
Mathias's answear is great, there's just one issue.
You first need to load the System.Windows.Forms assembly, as this article states!

How can I automate an existing instance of Internet Explorer using Perl?

I am struggling to get control of an IE preview control which is 'Internet Explorer_Server' class on an external windows application with perl.
Internet Explorer_Server is the class name of the window, I've found it with Spy++. and here’s my assertion code of it
$className = Win32::GUI::GetClassName($window);
if ($className eq "Internet Explorer_Server") {
...
}
I can get a handle of that 'Internet Explorer_Server' with Win32::GUI::GetWindow, but have no idea what to do next.
Updated: You are going down the wrong path. What you need is Win32::OLE.
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;
$Win32::OLE::Warn = 3;
my $shell = get_shell();
my $windows = $shell->Windows;
my $count = $windows->{Count};
for my $item ( 1 .. $count ) {
my $window = $windows->Item( $item );
my $doc = $window->{Document};
next unless $doc;
print $doc->{body}->innerHTML;
}
sub get_shell {
my $shell;
eval {
$shell = Win32::OLE->GetActiveObject('Shell.Application');
};
die "$#\n" if $#;
return $shell if defined $shell;
$shell = Win32::OLE->new('Shell.Application')
or die "Cannot get Shell.Application: ",
Win32::OLE->LastError, "\n";
}
__END__
So, this code finds a window with a Document property and prints the HTML. You will have to decide on what criteria you want to use to find the window you are interested in.
ShellWindows documentation.
You may want to have a look at Win32::IE::Mechanize. I am not sure whether you can control an existing IE window with this module, but accessing a single URL should be possible in about five lines of code.
Have you looked at Samie http://samie.sourceforge.net/ as this is a perl module to control IE

How can I get forking pipes to work in Perl on Windows?

I'm trying to port a Perl script over from Unix to Windows but am having a near impossible time getting it to work due to the unsupported forking pipes in the open function. Here's the code:
sub p4_get_file_content {
my $filespec = shift;
return 'Content placeholder!' if ($options{'dry-run'});
debug("p4_get_file_content: $filespec\n");
local *P4_OUTPUT;
local $/ = undef;
my $pid = open(P4_OUTPUT, "-|");
die "Fork failed: $!" unless defined $pid;
if ($pid == 0) { # child
my $p4 = p4_init();
my $result = undef;
$result = $p4->Run('print', $filespec);
die $p4->Errors() if $p4->ErrorCount();
if (ref $result eq 'ARRAY') {
for (my $i = 1; $i < #$result; $i++) {
print $result->[$i];
}
}
$p4->Disconnect();
exit 0;
}
my $content = <P4_OUTPUT>;
close(P4_OUTPUT) or die "Close failed: ($?) $!";
return $content;
}
The error is:
'-' is not recognized as an internal or external command,
operable program or batch file.
Does anyone know how to make this work? Thanks!
Mike
I know it's not a direct answer to your question, but it looks like you're scripting something on top of Perforce in Perl? If so you might find an existing library does what you want already and save yourself a lot of headaches, or at least give you some sample code to work from.
For example:
P4Perl
P4::Server
P4::C4
EDIT: Now that I know what you're doing I'm guessing you're trying to port p42svn to Windows, or rather make it compatible with Windows at least. See this thread for a discussion of this exact issue. The recommendation (untested) is to try the code samples listed at http://perldoc.perl.org/perlfork.html under "Forking pipe open() not yet implemented" to explicitly create the pipe instead.
It's not going to work as-is. You'll need to find another method to accomplish what it's doing. It doesn't look like there's that burning a need for the fork-pipe, but it's hard to tell since I don't know what a p4 is and a lot of your code is being lost to angle bracket interpretation.

Resources