Powershell > File not found - powershell

The file is present but Powershell keeps saying file is missing. I have no idea. I looked at the permissions and current user has full rights.
$csvDocLib = "C:\\PowerShell\TestLib.csv"
$csvDocSet = "C:\\PowerShell\TestDocSet.csv"
the first csv is found and code works but down the code line i need the second csv and it said not found but it's in same directory. I tried renaming and still the same.
if([IO.File]::Exists($csvDocSet) -ne $false)
{
write-host $csvDocSet " not found"
exit
}

Eliminate the double \\ e.g.:
$csvDocLib = "C:\\PowerShell\TestLib.csv"
$csvDocSet = "C:\\PowerShell\TestDocSet.csv"
should be:
$csvDocLib = "C:\PowerShell\TestLib.csv"
$csvDocSet = "C:\PowerShell\TestDocSet.csv"

Related

PowerShell: How to clear cache of included files?

I include an external .ps1 into antother .ps1:
foo.ps1:
.("C:\test\bar.ps1");
$obj = [bar]::new();
$obj.out();
bar.ps1:
class bar{
$output;
bar(){
$this.output = 1;
}
[void] out(){
write-host $this.output;
}
}
The first time I execute foo.ps1 in the Windows PowerShell ISE the output is "1", as expected.
Then I go to bar.ps1 and change $this.output = 1; to $this.output = 2;. After executing foo.ps1 again the output is still "1". When I change something in foo.ps1, like simply appending a new line, and execute it once again, the output becomes "2". Changing back, like removing the new line, will make an output of "1" again.
For me it looks like an caching issue. Is it possible to clear or disable the caching?
Thanks in advance!

Accessing array outside of a PS function

I am having a hard time figuring out how to get the PSCustomObject/array out of the function. I have tried using $Global:ZipList as well as just passing the variables into an array directly w/o a custom object but no luck. The reason I need this, is I need to then loop through the array/list after I get the filenames and then was going to loop through this list and unzip each file and log it and process it based on the extension in the zip; this is to be used for multiple zips, so I can't predetermine the file extensions without grabbing the filenames in the zip into a list. I would just use a shell however some of the zips are password protected, haven't figured out how to pass a password scripted to the shell com unzip windows feature so stuck with 7z for now. Any help would be greatly appreciated! Thanks
Function ReadZipFile([string]$ZipFileName)
{
[string[]]$ReadZipFile = & 'C:\Program Files\7-Zip\7z.exe' l "$ZipFileName"
[bool]$separatorFound = $false
#$ZipList = #()
$ReadZipFile | ForEach-Object{
if ($_.StartsWith("------------------- ----- ------------ ------------"))
{
if ($separatorFound)
{
BREAK # Second separator; We're done!
}
$separatorFound = -not $separatorFound
}
else
{
if ($separatorFound)
{
[DateTime]$FileCreatedDate = [DateTime]::ParseExact($_.Substring(0, 19),"yyyy'-'MM'-'dd HH':'mm':'ss", [CultureInfo]::InvariantCulture)
[Int]$FileSize = [Int]"0$($_.Substring(26, 12).Trim())"
$ZipFileName = $_.Substring(53).TrimEnd()
$ZipList = [PSCustomObject] #{
ZipFileName=$ZipFileName
FileCreatedDate=$FileCreatedDate
FileSize=$FileSize}
}
}
}
}
$z = ReadZipFile $ZipFileName
$ZipList | Select-Object ZipFileName
To be able to select from array created in the function outside of it. I believe my if statements may be blocking the global variable feature when i tried using global:

Comment lines in .sln file

I'm trying to comment some lines in .sln file temporarly, but I receive error:
"The selected file is a solution file, but appears to be corrupted and cannot be opened"
According to this blog comments are done by "#", but when I comment out every line in GlobalSection (section about Team Foundation Server source control binding) I get above error. Is there any other way to comment out lines in .sln ?
EDIT - section of which I want to comment out:
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 2
SccEnterpriseProvider = {4BA58AB2-18FA-4D8F-95F4-32FFDF27D184C}
SccTeamFoundationServer = http://oxy:8080/tfs/projects
SccLocalPath0 = .
SccProjectUniqueName1 = Accounts\\Accounts.vbproj
SccProjectName1 = Accounts
SccLocalPath1 = Accounts
EndGlobalSection
I tried this, but not working:
# GlobalSection(TeamFoundationVersionControl) = preSolution
# SccNumberOfProjects = 2
# SccEnterpriseProvider = {4BA58AB2-18FA-4D8F-95F4-32FFDF27D184C}
# SccTeamFoundationServer = http://oxy:8080/tfs/projects
# SccLocalPath0 = .
# SccProjectUniqueName1 = Accounts\\Accounts.vbproj
# SccProjectName1 = Accounts
# SccLocalPath1 = Accounts
# EndGlobalSection
P.S.: I tried a single line comment using "#" - that works. And removing whole section also works. But I don't want to delete It, just comment It.
I don't think there is an official definition of comments in the .sln file. It depends on the parser.
In principle, a .sln file is a declarative file, based on the keywords (ProjectSection, EndGlobalSection) and the end-line char. I don't see a uniform format description.
MSBuild
So we don't know how Visual Studio reads the .sln file, but you can see in the msbuild code that any line that doesn't start with one of the const words will not enter to the object:
while ((str = ReadLine()) != null)
{
if (str.StartsWith("Project(", StringComparison.Ordinal))
{
ParseProject(str);
}
else if (str.StartsWith("GlobalSection(NestedProjects)", StringComparison.Ordinal))
{
ParseNestedProjects();
}
else if (str.StartsWith("GlobalSection(SolutionConfigurationPlatforms)", StringComparison.Ordinal))
{
ParseSolutionConfigurations();
}
else if (str.StartsWith("GlobalSection(ProjectConfigurationPlatforms)", StringComparison.Ordinal))
{
rawProjectConfigurationsEntries = ParseProjectConfigurations();
}
else if (str.StartsWith("VisualStudioVersion", StringComparison.Ordinal))
{
_currentVisualStudioVersion = ParseVisualStudioVersion(str);
}
else
{
// No other section types to process at this point, so just ignore the line
// and continue.
}
}
Visual Studio
According to this blog comments are done by "#"
That is not accurate. You can add lines with any text where you want, without the #, and the file will stay correct.
So, in Visual Studio, you currently don't know the official format, but you need to "break" the current format.
You can try changing the keywords, such as adding a letter to them at the beginning.
From what I've tried, special characters (such as #, %) don't break your keywords.

Why weird assignment from variable inside Powershell switch statement?

I'm a beginner at Powershell and am struggling to understand some syntax from some code I found on Github. I've read the docs on Powershell assignment, and on switch statements, and can't understand what is going on with the = $Yes and = $No in this code snippet:
Switch ($Prompt3) {
Yes {
Stop-EdgePDF
Write-Output "Edge will no longer take over as the default PDF viewer."; = $Yes
}
No {
= $No
}
}
I haven't been able to find any references to this kind of syntax, and it doesn't seem to do anything in the script. So why is it there?
UPDATE: This issue has been resolved.
Looks to me like the variable name that was getting the assignment was deleted in a change back in August.
$PublishSettings = $Yes
Was changed to:
= $Yes
And:
$PublishSettings = $No
Was changed to:
= $No
Looks like poor search and replace.
I've created an issue for the problem at GitHub.
There are many characters that are valid in a function (or variable) name; this includes the = symbol. What you're observing is a function or alias.
Examples:
# standard function
function =
{
return $args
}
# accessing the function: drive
${Function:=} = {
return $args
}
# defining a new alias
New-Alias -Name = -Value Get-Variable
# using the Alias attribute
function Test-Thing
{
[Alias('=')]
param()
return $args
}

moodle 2.7 filemanager issue

I'm working with a file manager on a form in moodle 2.7.
The save and upload files features are fine.
I need to determine if the file manager object currently holds a file.
This is what I've tried:
if($draftitemid = file_get_submitted_draft_itemid('attachments')){
$A=1;
}else{
$A=2;
}
But it always return 1;
[SOLVED]
Just after saveing the form files and before update record I use:
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, $component,$path, $itemid,'',false);
if(!empty($files){
$A=1;//have files
}else{
$A=2;//No files
}
this work for me.
This line:
$draftitemid = file_get_submitted_draft_itemid('attachments')
Sets $draftitemid to the value returned by file_get_submitted_draft_itemid('attachments'). Setting a variable always evaluates to true when in an if statement. Thus, this is a typo and what you want is:
if($draftitemid == file_get_submitted_draft_itemid('attachments')){