Creating a symbolic link using PowerShell

huangapple go评论58阅读模式
英文:

Creating a symbolic link using PowerShell

问题

I have provided the translated code portions below:

我在使用PowerShell方面遇到了一些困难我觉得应该可以很容易地完成希望能得到一些帮助

我有一系列存储在`C:\temp`内的日志和配置文件路径不仅如此我需要在每次用户运行应用程序通过批处理文件启动时删除临时文件然后创建一个符号链接到登录用户的OneDrive中

因此我需要运行大约10个文件夹如下所示

```none
rmdir /Q /S c:\temp\app\cgi-bin
rmdir /Q /S c:\temp\app\contrib
rmdir /Q /S c:\temp\app\logs 

等等

...然后运行以下相应的命令:

mklink /D c:\temp\app\cgi-bin c:\users\<logged on user>\One Drive - Company Name\app\cgi-bin
mklink /D c:\temp\app\contrib c:\users\<logged on user>\One Drive - Company Name\app\contrib
mklink /D c:\temp\app\logs c:\users\<logged on user>\One Drive - Company Name\app\logs

我尝试在PowerShell中完成这个任务,如下所示:

$username = $env:username

(get-item C:\temp\app\cgi-bin).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\cgi-bin -target 'C:\users$username\OneDrive - Company Name\app\cgi-bin\'

...然后将它运行到每个位置:

$username = $env:username

(get-item C:\temp\app\contrib).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\contrib -target 'C:\users$username\OneDrive - Company Name\app\contrib\'

等等

但是我感到非常困惑。我觉得应该有一种简单的方法来循环遍历原始文件夹位置,然后将它们连接在一起。我尝试使用join-path,但也没有成功:

New-item -ItemType SymbolicLink -path C:\temp\app\cgi-bin -target (join-path 'C:\users' '"OneDrive - Company Name\app\cgi-bin\"')

请注意,我已将原文中的HTML实体编码符号(如`&lt;`和`&gt;`)保留在翻译中,以保持文本的一致性。这些符号通常用于表示尖括号(`<`和`>`)。如果需要进一步的协助,请随时提出。

<details>
<summary>英文:</summary>

I am struggling with a bit of PowerShell, which I feel I should be able to do easily, and would appreciate some help.

I have a series of paths storing log and config files within `C:\temp`. Not only that, but I need to delete the temp file and then create a symbolic link to the logged on user&#39;s OneDrive every time they run the app (which is launched with a batch file).

So I need it to run through about 10 folders such as

```none
rmdir /Q /S c:\temp\app\cgi-bin
rmdir /Q /S c:\temp\app\contrib
rmdir /Q /S c:\temp\app\logs 

etc.

...and then run the equivalent of:

mklink /D c:\temp\app\cgi-bin c:\users\&lt;logged on user&gt;\One Drive - Company Name\app\cgi-bin
mklink /D c:\temp\app\contrib c:\users\&lt;logged on user&gt;\One Drive - Company Name\app\contrib
mklink /D c:\temp\app\logs c:\users\&lt;logged on user&gt;\One Drive - Company Name\app\logs

What I have attempted in PS to do this is below:

$username = $env:username

(get-item C:\temp\app\cgi-bin).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\cgi-bin -target &#39;C:\users$username\OneDrive - Company Name\app\cgi-bin\&#39;

...and then was going to get it to run the same for each location:

$username = $env:username

(get-item C:\temp\app\contrib).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\contrib -target &#39;C:\users$username\OneDrive - Company Name\app\contrib\&#39;

etc.

...but have got completely lost. I feel there should be an easy way to loop through the original folder locations and then joining the parts together. I had another go with join-path, but it didn't go too well either:

New-item -ItemType SymbolicLink -path C:\temp\app\cgi-bin -target (join-path &#39;C:\users&#39; &#39;&quot;OneDrive - Company Name\app\cgi-bin\&quot;&#39;)

答案1

得分: 0

以下是您要翻译的部分:

"Just, first, as you're new here -- so Welcome ! --, it doesn't really matter, but keep in mind to post the errors you are getting, because I had to figure them out here 😃.

Right, just a few things first :

  • to loop through an array, you can use the foreach(){} method.
  • as @Compo stated, you can use $env:onedrive to get the current user's OneDrive directory ($env is the environment variable, it's a preset system-wide variable basically containing user-useful settings)
  • you cannot create a symbolic link pointing to a non-existing directory, so here, if the folders in OneDrive don't exist, it won't work; so I'm adding a line here to check if the folder exists, and create it if not, just make sure app exists !
$onedrive = "$env:onedrive - Company Name"
foreach ($folder in (Get-ChildItem -Path "C:\temp\app").Name){
    Remove-Item -Path $folder
    $onedrivePath = "$onedrive\app$folder"
    if (-not(Test-Path $onedrivePath)){
        New-Item -ItemType Directory -Path $onedrivePath
    }
    New-item -ItemType SymbolicLink -Path "c:\temp\app$folder" -target $onedrivePath
}

Feel free to comment if you want more explanations, and have a wonderful PowerShell journey (PowerShell is one of those incredible but underestimated languages...)

Silloky

EDIT :

After modifications to the script as you requested, here it is in all its beauty !

$onedrive = "$env:onedrive"
$pathArray = @(
    "contrib", 
    "htdocs", 
    "install", 
    "licenses", 
    "locale", 
    "apache\logs", 
    "mysql\data", 
    "mysql\backup", 
    "perl", 
    "php", 
    "phpMyAdmin", 
    "sendmail", 
    "tmp", 
    "tomcat", 
    "webdav"
)

foreach ($path in $pathArray) {
    $localpath = "c:\app$path"
    if (Test-Path $localpath) {
        Remove-Item -path $localpath -Recurse -force -Confirm:$false
    }
    # Write-Output "Setting OneDrive Path"
    $onedrivePath = "$onedrive\app$path"
    if (-not(Test-Path $onedrivePath)) {
        New-Item -ItemType Directory -Path $onedrivePath
    }
    # Write-Output "Creating Junction"
    New-item -ItemType Junction -Path "$localpath" -target $onedrivePath
}

I replaced Read-Host by Write-Output as it is more adequate : Read-Host prompts for user input (like input() in Python) when Write-Output prints to the console (like print() in Python), which is what you want assuming from the "prompt" texts.

I've also organised the array in much more semantic way, making it easier to edit.

Silloky

英文:

Just, first, as you're new here -- so Welcome ! --, it doesn't really matter, but keep in mind to post the errors you are getting, because I had to figure them out here 😅.

Right, just a few things first :

  • to loop through an array, you can use the foreach(){} method.
  • as @Compo stated, you can use $env:onedrive to get the current user's OneDrive directory ($env is the environment variable, it's a preset system-wide variable basically containing user-useful settings)
  • you cannot create a symbolic link pointing to a non-existing directory, so here, if the folders in OneDrive don't exist, it won't work; so I'm adding a line here to check if the folder exists, and create it if not, just make sure app exists !
$onedrive = &quot;$env:onedrive - Company Name&quot;
foreach ($folder in (Get-ChildItem -Path &quot;C:\temp\app&quot;).Name){
    Remove-Item -Path $folder
    $onedrivePath = &quot;$onedrive\app$folder&quot;
    if (-not(Test-Path $onedrivePath)){
        New-Item -ItemType Directory -Path $onedrivePath
    }
    New-item -ItemType SymbolicLink -Path &quot;c:\temp\app$folder&quot; -target $onedrivePath
}

Feel free to comment if you want more explanations, and have a wonderful PowerShell journey (PowerShell is one of those incredible but underestimated languages...)

Silloky

EDIT :

After modifications to the script as you requested, here it is in all its beauty !

$onedrive = &quot;$env:onedrive&quot;
$pathArray = @(
    &quot;contrib&quot;, 
    &quot;htdocs&quot;, 
    &quot;install&quot;, 
    &quot;licenses&quot;, 
    &quot;locale&quot;, 
    &quot;apache\logs&quot;, 
    &quot;mysql\data&quot;, 
    &quot;mysql\backup&quot;, 
    &quot;perl&quot;, 
    &quot;php&quot;, 
    &quot;phpMyAdmin&quot;, 
    &quot;sendmail&quot;, 
    &quot;tmp&quot;, 
    &quot;tomcat&quot;, 
    &quot;webdav&quot;
)

foreach ($path in $pathArray) {
    $localpath = &quot;c:\app$path&quot;
    if (Test-Path $localpath) {
        Remove-Item -path $localpath -Recurse -force -Confirm:$false
    }
    # Write-Output &quot;Setting OneDrive Path&quot;
    $onedrivePath = &quot;$onedrive\app$path&quot;
    if (-not(Test-Path $onedrivePath)) {
        New-Item -ItemType Directory -Path $onedrivePath
    }
    # Write-Output &quot;Creating Junction&quot;
    New-item -ItemType Junction -Path &quot;$localpath&quot; -target $onedrivePath
}

I replaced Read-Host by Write-Output as it is more adequate : Read-Host prompts for user input (like input() in Python) when Write-Output prints to the console (like print() in Python), which is what you want assuming from the "prompt" texts.

I've also organised the array in much more semantic way, making it easier to edit.

Silloky

答案2

得分: 0

$onedrive = "$env:onedrive"
$pathvariable = @("contrib", "htdocs", "install", "licenses", "locale", "apache\logs", "mysql\data", "mysql\backup", "perl", "php", "phpMyAdmin", "sendmail", "tmp", "tomcat", "webdav")

foreach ($var in $pathvariable) {
$localpath = "c:\app$var"

if (Test-Path $localpath){

Remove-Item -path $localpath -Recurse -force -Confirm:$false
}

##Read-Host -Prompt "Setting OneDrive Path"
$onedrivePath = "$onedrive\app$var"

if (-not(Test-Path $onedrivePath)){
    New-Item -ItemType Directory -Path $onedrivePath
}

##Read-Host -Prompt "Creating Junction"

New-item -ItemType Junction -Path &quot;$localpath&quot; -target $onedrivePath
}
英文:
$onedrive = &quot;$env:onedrive&quot;
$pathvariable = @(&quot;contrib&quot;, &quot;htdocs&quot;, &quot;install&quot;, &quot;licenses&quot;, &quot;locale&quot;, &quot;apache\logs&quot;, &quot;mysql\data&quot;, &quot;mysql\backup&quot;, &quot;perl&quot;, &quot;php&quot;, &quot;phpMyAdmin&quot;, &quot;sendmail&quot;, &quot;tmp&quot;, &quot;tomcat&quot;, &quot;webdav&quot;)

foreach ($var in $pathvariable) {
$localpath = &quot;c:\app$var&quot;
         
   if (Test-Path $localpath){
        
Remove-Item -path $localpath -Recurse -force -Confirm:$false
    }

##Read-Host -Prompt &quot;Setting OneDrive Path&quot;
$onedrivePath = &quot;$onedrive\app$var&quot;

    if (-not(Test-Path $onedrivePath)){
        New-Item -ItemType Directory -Path $onedrivePath
    }

##Read-Host -Prompt &quot;Creating Junction&quot;

    New-item -ItemType Junction -Path &quot;$localpath&quot; -target $onedrivePath
    }

huangapple
  • 本文由 发表于 2023年3月4日 01:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定